Just run into a tricky NSFetchedResultsController problem.
The following code works fine in all cases EXCEPT for the very first entry to the Core Data database when it reports 2 rows!
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
numberOfRows = [sectionInfo numberOfObjects];
If I add additional entries these are reported correctly. And if I delete both of the 2 initial rows it works fine.
Any suggestions? If it's any help, by using:
-com.apple.CoreData.SQLDebug 1
I can see there was 1 INSERT and 2 SELECTs.
Also, this problem doesn't seem happen if I've already visited the view containing the NSFetchedResultsController code (i.e. before doing the INSERTs).
====
UPDATE 1:
Wonder if this walk through the code will help...
1. ViewController A starts a background NSOperation which does a download
2. When this completes it sends a notification to the AppDelegate
3. When the AppDelegate gets this notification it imports the data (doing a check to make sure it's on the main thread before doing so)
Here's the relevant bit of the importerDidSave code (where this technique was taken from):
- (void)importerDidSave:(NSNotification *)saveNotification {
NSLog(@"In importerDidSave...");
if ([NSThread isMainThread]) {
NSLog(@"... on Main Thread.");
NSLog(@"Number of NSFetchedResultsController rows BEFORE: %d", [[[fetchedResultsController sections] objectAtIndex:0] numberOfObjects]);
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:saveNotification];
NSLog(@"Number of NSFetchedResultsController rows AFTER: %d", [[[fetchedResultsController sections] objectAtIndex:0] numberOfObjects]);
which outputs:
... on Main Thread.
Number of NSFetchedResultsController rows BEFORE: 1
Number of NSFetchedResultsController rows AFTER: 3