Search code examples
ioscore-dataafincrementalstore

AFIncrementalStore simple fetch terminating with a crash


I'm setting up a very simple NSIncrementalStore example using AFIncrementalStore.

The idea is to setup a NSManagedObjectContext in the AppDelegate (using the ordinary template provided by Apple, with changes for my IncrementalStore), do a fetch with no predicate or sort descriptor and NSLog the one fetched entity object.

Everything works great until I ask for any entity attribute. It crashes with the following message:

2013-07-22 16:34:46.544 AgendaWithAFIncrementalStore[82315:c07] -[_NSObjectID_id_0 eventoId]: unrecognized selector sent to instance 0x838b060
2013-07-22 16:34:46.545 AgendaWithAFIncrementalStore[82315:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_id_0 eventoId]: unrecognized selector sent to instance 0x838b060'

My xcdatamodeld is correctly setted up. The NSManagedObject class is generated and imported on the delegate. When I do a breakpoint before the NSLog I can see the fetched objects IDs. The webservice is giving me back the correct data.

My AppDelegate code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ... 
    [self.window makeKeyAndVisible];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remoteFetchHappened:) name:AFIncrementalStoreContextDidFetchRemoteValues object:self.managedObjectContext];

    NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Agenda" inManagedObjectContext:self.managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.entity = entityDescription;
    fetchRequest.predicate = nil;
    NSError *error;

    [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    return YES;
}

// Handle the notification posted when the webservice returns objects
- (void)remoteFetchHappened:(NSNotification *)aNotification
{
    NSArray *fetchResult = [[aNotification userInfo] objectForKey:@"AFIncrementalStoreFetchedObjectIDs"];
    Agenda *agenda = (Agenda *)[fetchResult lastObject];

    // THIS IS WHERE IT BREAKS...
    NSLog(@"Agenda: %@", agenda.eventoId);
}

Any ideas on how to make this piece of code return the attribute I'm asking for?


Solution

  • AFNetworking is giving you managed object IDs, that is, instances of NSManagedObjectID. You can't look up managed object property values on that-- you have to get the managed object for the ID first. That's what _NSObjectID_id_0 means in the error message-- you're trying to get eventoId on an NSManagedObjectID, and it has no idea what that is.

    You get the managed object by looking it up in the managed object context. Something like

    NSError *error = nil;
    NSManagedObject *myObject = [context existingObjectWithID:objectID error:error];
    if (myObject != nil) {
        // look up attribute values on myObject
    }