Search code examples
ioscore-datadelegatesnsfetchedresultscontrollercore-data-migration

NSFetchResultsController delegate not getting called


I am using theNSFetchedResultsController for loading my tableView using coredata. Recently I have added a new Entity in my ManagedObjectModel and did coredata versioning and used light weight migration.

A print description of my Managed Object Context (MOC) shows the newly added Schema and schema is also defined in the NSPersistentStoreCoordinator.

My issue is that once I insert data in to the newly created Entity the NSFetchedResultsController delegates are not getting fired even though no error is thrown while insertion and I verified that insertions are indeed taking place in the SQL table.

I have checked whether coredata migration is required using

 NSMigrationManager *manager = [[NSMigrationManager alloc]
                               initWithSourceModel:[self sourceModel] destinationModel:[self destinationModel]];




BOOL success = [manager migrateStoreFromURL:storeURL type:NSSQLiteStoreType
                                    options:nil withMappingModel:mappingModel toDestinationURL:dstStoreURL
                            destinationType:NSSQLiteStoreType destinationOptions:nil error:outError];

and found that coredata is automatically selects the correct model and there is no need to use NSMigrationManager.

For implementing NSFetchResultsController i have followed examples from Raywenderlich

http://www.raywenderlich.com/999/core-data-tutorial-how-to-use-nsfetchedresultscontroller and it used to work fine on CoreData without any versioning.

Can anyone tell me how to make NSFetchResultsController delegate work after adding a new Entity and doing versioning and I have changed the Current Core Data Model to the latest one .There are no relation between newly added Entity and Old Entity in the Managed Object Model.

EDIT:Here is my fetch results controller initialisation code

- (NSFetchedResultsController *)fetchedResultsController {

if (fetchedResultsController != nil) {
    return fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"SongDetails" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"songName" ascending:YES];


[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];



[fetchRequest setFetchBatchSize:10];

/*CHECKED WHETHER ANY SONGDETAILS ARE FETCHED*/
NSError * err;
NSArray * results = [managedObjectContext executeFetchRequest:fetchRequest error:&err];
NSLog(@" results >>> %@ ,results);
/* */

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
                                               cacheName:nil];

self.fetchedResultsController = theFetchedResultsController;
fetchedResultsController.delegate = self;

return fetchedResultsController;

}

After I added a Log in the fetch results controller I am getting the following results

 results >>> ("<SongDetails: 0x115abae0> (entity: SongDetails; id: 0x115a94a0 <x-coredata://7E5CDD40-8A4C-455D-8000-34FD70AC8837/SongDetails/p1> ; data: <fault>)",
"<SongDetails: 0x115abe90> (entity: SongDetails; id: 0x115aa450 <x-coredata://7E5CDD40-8A4C-455D-8000-34FD70AC8837/SongDetails/p2> ; data: <fault>)",

Again following are the results from the SQL debugger while insertion.

2013-03-06 12:09:25.894[6035:c07] CoreData: sql: BEGIN EXCLUSIVE
2013-03-06 12:09:25.895[6035:c07] CoreData: sql: INSERT INTO ZSONGDETAILS(Z_PK, Z_ENT, Z_OPT, ZSONGNAME) VALUES(?, ?, ?, ?)
2013-03-06 12:09:25.902[6035:c07] CoreData: sql: COMMIT

Any help will be appreciated.


Solution

  • Paraphrasing from my comments. You need to call [fecthedResultsController performFetch:&error] before the FRC will detect model changes and call its delegate methods.