0
votes

Using MagicalRecord version 2.3 NSFetchedResultsController initWithFetchRequest does not return any objects, despite knowing there are objects available.

NSFetchRequest * fetchRequest = [SomeObject MR_requestAllSortedBy:@"created" ascending:NO];
//[request setFetchLimit:100];
[request setFetchBatchSize:20];

NSManagedObjectContext* managedObjectContext = [NSManagedObjectContext MR_defaultContext];
NSFetchedResultsController* fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest managedObjectContext: managedObjectContext sectionNameKeyPath:nil cacheName:nil];

NSInteger rowCount = fetchedResultsController.fetchedObjects.count

What am i missing?

Thanks, Mike

1
What is the code that is not returning any objects? Also, format your code with CMD-K. - Fogmeister
Also, are you running performFetch on the FRC? - Fogmeister
Sorry code formatted now. Just checking the returning rows with fetchedResultsController.fetchedObjects.count returns nil - MichealB1969
And yes the fetch is running on the TableView Controller - MichealB1969
Cool, so when you say that no objects are being returned... Which bit is telling you that? What bit of code? And how do you know that it knows about the objects? The fetch needs to run on the fetched results controller. You should have the line [fetchedResultsController performFetch:... do you have that somewhere? - Fogmeister

1 Answers

2
votes

After creating the FRC you need to make sure you run the performFetch method. This is the method that actually tells the FRC to go and get the objects.

if (![fetchedResultsController performFetch:&someError]) {
    // handle the error.
}

If you're using MagicalRecord then you can save a lot of code by doing...

NSFetchedResultController *frc = [SomeObject MR_fetchAllSortedBy:@"created" ascending:NO];
[frc performFetch:nil];

LOL! Just searching around and found this site... http://samwize.com/2014/03/29/implementing-nsfetchedresultscontroller-with-magicalrecord/

Seems you got there before me.