Search code examples
ios5xcode4uiviewcontrollernsmanagedobjectcontextnsfetchrequest

Getting SIGABRT when executing fetchRequest


I'm using Xcode 4.2 and iOS SDK 5.0 and Apple's Master-Detail Application Template for iPad with Core Data. It's similar to the "Locations" sample code. I've successfully managed to pass the managed object context (MOC) from the MasterViewController (MVC) to the DetailViewController (DVC). The detail view controller accepts input from the user in some text boxes and stores that in core data; this part works like a charm. Now, I have a ActionViewController (AVC) that is a Popover View that is supposed to allow the user to e-mail all of the data in the MOC if they choose so. However, when trying to do a fetch I get a SIGABRT. I used breakpoints to pinpoint exactly where:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Fetch the crosses
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event"
                                                          inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; // Where it crashes

    //Other code
}

Also, in the debugger I see that the *context pointer is 0x0 which I'm guessing means that it's not there?

The way I made the MVC pass the MOC was like this:

// Pass the managedObjectContext to the DetailViewController as a property
_detailViewController.managedObjectContext = self.managedObjectContext;

//Pass the MOC to the actionViewController
_actionViewController.managedObjectContext = self.managedObjectContext;

I've looked at other posts and it seems that passing the MOC to all the view controllers from the AppDelegate may be the way to go but I wanted to find out why the data input works for the DVC but crashes with fetching data in the AVC. What grand error have I made?

Fixed link to github project: https://github.com/scottdaniel/fly_punnett


Solution

  • So I figured it out, though I'm probably breaking some "good programming" maxim...

    All I did was delete

    //Pass the MOC to the actionViewController
    _actionViewController.managedObjectContext = self.managedObjectContext;
    

    along with all other connections from AVC to MVC (i.e. @class ActionViewController, @property *ActionViewController in the MCV interface) << ask me for the full details if you want them or check it out on github (link above)

    Then I just added

    #import AppDelegate.h
    

    to my ActionViewController.m

    and modified the MOC creation like so:

    -(void)tableView:(UITableView *)tableView 
                      didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Fetch the crosses
    NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication 
                                        sharedApplication] delegate] 
                                        managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"Event"
                                   inManagedObjectContext:context];
    

    The MOC is created successfully and I'm able take the data for the user and put it into an e-mail they can send to whoever.