Note that there are some explanatory texts on larger screens.

plurals
  1. PONSFetchedResultsController fails to fetch in child context
    primarykey
    data
    text
    <p>I have a <code>UIManagedDocument</code> with some data which I display in a list using <code>NSFetchedResultsController</code>. The data is regularly updated in the background and changes are put onto the <code>UIManagedDocument.managedObjectContext</code> (using performBlock:).</p> <p>When I display the data from the main context of the document, all works as expected. But as soon as I display the list in a context which is a child of the main context (<code>child.parentContext = document.managedObjectContext</code>), I don't see any objects and the following error is printed on the console:</p> <pre><code>foo[17895:15203] CoreData: error: (NSFetchedResultsController) The fetched object at index 5 has an out of order section name 'E. Objects must be sorted by section name' </code></pre> <p>This only happens after a new object was inserted into the documents contact. When I wait long enough for the automatic save to happen, the list displays fine. Also the problem is only when I have a <code>sectionNameKeyPath</code> set on the <code>NSFetchedResultsController</code> and only with the child context.</p> <p>This is how I setup the fetched results controller, nothing fancy so I don't see what I could do wrong here:</p> <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Contact"]; fetchRequest.sortDescriptors = [Contact userDefinedSortDescriptors]; fetchRequest.predicate = [NSPredicate predicateWithFormat:@"hidden == nil || hidden == NO"]; NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_managedObjectContext sectionNameKeyPath:[Contact userDefinedSectionNameKeyPath] cacheName:@"ContactList"]; </code></pre> <p><code>[Contact userDefinedSortDescriptors]</code> and <code>[Contact userDefinedSectionNameKeyPath]</code> are resolved at runtime. The sort descriptors contain as first entry the sectionNameKeyPath. Neither can be <code>nil</code> or other funny stuff.</p> <p><strong>Edit</strong>: Clarified some vague parts. Specifically, I don't call -save: on the documents managed object context.</p> <p><strong>Edit 2</strong>: I'll try to explain how the MOCs relate to each other.</p> <p>There are three managed object contexts in play:</p> <p>1) The <code>UIManagedDocument.managedObjectContext</code> created by loading the document.</p> <p>2) There is a background thread running which updates the objects from time to time. This is a private queue moc with the documents MOC as the parent:</p> <pre><code>context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; context.parentContext = repository.managedObjectContext; [context performBlock:^{ /* updates */ }]; [context performBlock:^{ [context save:NULL]; }]; </code></pre> <p>3) When the user wants to make changes a new MOC is created as a child of the document MOC. This is a main queue MOC. This is the context which is used to perform the fetch shown above.</p> <p>The background updates are done from an NSOperationQueue but all access to the background MOC is properly surrounded with a <code>-performBlock:</code>. All other accesses are done from the main thread.</p> <p><strong>Edit 3</strong>: While playing around with some settings on <code>NSFetchRequest</code> I found that the problem goes away when setting <code>fetchRequest.includesPendingChanges = NO</code>. But this is not a viable solution, because now the user doesn't see any updates anymore until the changes are saved in the background by UIManagedDocument.</p>
    singulars
    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