Reputation: 547
I'm using a TableViewController class that is much like the one that is created when you start a new Master-Detail Application project in Xcode. As such, I'm using the same code that is pre-populated in the TableViewController class for my own use. However, I'm getting a runtime crash and I'm not sure why. I use this exact code in another class of my app and it works perfectly.
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Binder" inManagedObjectContext:[appDelegate managedObjectContext]];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
//NSArray *sortDescriptors = @[sortDescriptor];
//[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
//This is where it crashes
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[appDelegate managedObjectContext] sectionNameKeyPath:nil cacheName:@"Master"];
//End crash
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
I'm not sure what other code snippets to include here. Output doesn't tell me anything when the crash happens, and Xcode jumps to this part of the Main Thread:
libsystem_kernel.dylib`__kill:
0x972893b0: movl $786469, %eax
0x972893b5: calll 0x9728b4c2 ; _sysenter_trap
0x972893ba: jae 0x972893ca ; __kill + 26 //This is highlighted
0x972893bc: calll 0x972893c1 ; __kill + 17
0x972893c1: popl %edx
0x972893c2: movl 27739(%edx), %edx
0x972893c8: jmpl *%edx
0x972893ca: ret
0x972893cb: nop
Any thoughts? Thanks
Upvotes: 2
Views: 1357
Reputation: 547
Thanks to @flashfabrixx, the problem was that I was not using a sort descriptor and they are required when using a NSFetchedResultsController
. Once I added the sort descriptor back in, everything worked perfectly.
Upvotes: 6
Reputation: 7646
Passing a nil
managed object context to initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:
will throw an exception. I'm surprised you're not seeing anything in the console log though.
Try NSAssert()
to verify that your MOC, and the Binder
entity, are both non-nil.
If the cache name in your NSFetchedResultsController
has been used for another FRC, you'll see an error unless the two controllers' fetch requests are identical. Set a nil
(or different) cacheName:
and see if you get a different result.
Upvotes: 0
Reputation: 80265
Well, the only non-standard thing you are doing is that you are using a managed object context from the app delegate. This is really not recommended, for many good reasons.
Try changing this by adding a context property to your master controller and using that context to create your fetched results controller (both for getting a reference to the entity and for the FRC creation).
Finally, ensure your model indeed contains a valid Binder
entity.
Upvotes: 0