Note that there are some explanatory texts on larger screens.

plurals
  1. PONSSortDescriptor not being applied to fetch request results under iOS 5.1 (but works fine under iOS 6+)
    primarykey
    data
    text
    <p>I have a fetch request "foo" on which I have a sort descriptor (it sorts an Integer 16 attribute in descending order). I execute the fetch request thusly:</p> <pre><code>__block NSArray *results = nil; [_managedObjectContext performBlockAndWait: ^{ results = [_managedObjectContext executeFetchRequest:&lt;foo&gt; error:nil]; }]; return results; </code></pre> <p>When I execute this fetch request under iOS 6+, the results are sorted as specified/expected (reverse integer index order). </p> <p>However, when I execute it under iOS 5.1, the results are unsorted! </p> <p>But if I then immediately apply the exact same sort descriptor to the results array thusly:</p> <pre><code>results = [results sortedArrayUsingDescriptors:&lt;foo&gt;.sortDescriptors]; </code></pre> <p>the results are then sorted correctly.</p> <p>Has anyone else encountered anything like this? If so, did you discover the cause?</p> <p>Thanks!</p> <p>Carl</p> <p>P.S.: In case it proves relevant, here is the definition of "foo":</p> <pre><code>-(NSFetchRequest *)fetchRequestForUnparsedStoriesOfStoryListWithId:(id)storyListId { NSPredicate *hasStoryListWithIdPredicate = [NSPredicate predicateWithFormat:@"ANY lists.id = %@", storyListId]; NSPredicate *isUnparsedStoryOfStoryListWithIdPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[hasStoryListWithIdPredicate, self.isUnparsedPredicate]]; NSFetchRequest *fetchRequestForUnparsedStoriesOfStoryListWithId = [NSFetchRequest fetchRequestWithEntityName:@"Story"]; fetchRequestForUnparsedStoriesOfStoryListWithId.predicate = isUnparsedStoryOfStoryListWithIdPredicate; fetchRequestForUnparsedStoriesOfStoryListWithId.sortDescriptors = [NSArray arrayWithObject:self.descendingIndexSortDescriptor]; return fetchRequestForUnparsedStoriesOfStoryListWithId; } </code></pre> <p>and here is the definition of the sort descriptor:</p> <pre><code>-(NSSortDescriptor *)descendingIndexSortDescriptor { if (! _descendingIndexSortDescriptor) { self.descendingIndexSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:NO]; } return _descendingIndexSortDescriptor; } </code></pre> <p>P.P.S.: A little more context on, well, the context of this fetch: This is occurring on app launch, after 1) a background-thread/background-MOC (a nested MOC with NSPrivateQueueConcurrencyType) parse of server information has determined some basic properties, then 2) created a set of Story entities in the background MOC having (<em>inter alia</em>) the index being sorted upon, and then 3) saved the changes on that background MOC. Only, and immediately, after those operations complete is the above fetch request performed, and it is performed on the same background thread and background MOC. Having read here: <a href="https://stackoverflow.com/questions/10253817">NSSortdescriptor ineffective on fetch result from NSManagedContext</a> — that "unless the data is saved all the way back to Persistent Store, the sorting won't work if the data in the master context is dirty, i.e. modified", I tried forcing the save in step 3) all the way "up" the nested MOC chain to the persistent store (i.e., to disk), but that too had no effect: under iOS 5.1 (only) the results of this fetch are not sorted, unless I sort the results "manually" after performing the fetch.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload