I'm trying to use a NSFetchedResultsController with an additional NSManagedObjectContext different from the main MOC that I use in my App. Although I'm able to save and retrieve data with that additional MOC, every time that I try to create a NSFetchedResultsController it returns an object with nil fetchedObjects.
I'm using MagicalRecord, and this is the code that I'm using:
- (id) init
{
if(SINGLETON){
return SINGLETON;
}
if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd];
}
self = [super init];
if (self) {
NSManagedObjectModel *model = [NSManagedObjectModel MR_newManagedObjectModelNamed:@"ConnectorCache.momd"];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:model];
storeCoordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStoreNamed:@"ConnectorCache.sqlite"];
cacheContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:storeCoordinator];
[MagicalRecord setShouldAutoCreateManagedObjectModel:YES];
}
return self;
}
and the code for getting the fetchedResultsController is:
- (NSFetchedResultsController *) fetchedResultsController {
if (_fetchedResultController != nil) {
return _fetchedResultController;
}
NSFetchRequest *request = [Appointment MR_requestAllSortedBy:@"start" ascending:YES inContext:cacheContext];
_fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:cacheContext sectionNameKeyPath:@"day" cacheName:nil];
return _fetchedResultController;
}
If I use the request for fetching the data, it works correctly:
(lldb) po [cacheContext executeFetchRequest:request error:nil];
<_PFBatchFaultingArray 0x7fd9ca576710>(
<Appointment: 0x7fd9ca4c7220> (entity: Appointment; id: 0xd00000000004000a <x-coredata://36BACA76-7C2B-413C-8782-F92BBC7C1AA7/Appointment/p1> ; data: {
attendees = "<relationship fault: 0x7fd9ca518400 'attendees'>";
attendeesOmitted = nil;
created = "2015-03-10 16:41:31 +0000";
creator = nil;
end = "2015-03-18 18:45:00 +0000";
eventDescription = "Doctor Garc\U00eda Villaran";
eventID = "_8gs3ecpi8h0jab9i6op3ib9k6gp3iba18kojiba68d1j8c2260pj4e1o64";
lastUpdated = "2015-03-10 16:42:13 +0000";
location = "Centro M\U00e9dico Quir\U00f3n Sevilla Este, Sevilla, ES";
start = "2015-03-18 17:45:00 +0000";
status = 0;
title = "Cita otorrino";
})
)
I cannot see what I'm missing…
Kind regards
I was my fault, what I missed is the fact that I'm using MagicalRecord which actually provides a method for getting a NSFetchedResultsController.
NSManageObject MR_fetchAll… or friends will do the job. In my case I used:
_fetchedResultController = [Appointment MR_fetchAllSortedBy:@"start"
ascending:YES
withPredicate:nil
groupBy:@"day"
delegate:nil
inContext:cacheContext];