2013-07-22 30 views
0

我使用AFIncrementalStore设置了一个非常简单的NSIncrementalStore示例。AFIncrementalStore简单提取以崩溃终止

这个想法是在AppDelegate中设置一个NSManagedObjectContext(使用Apple提供的普通模板,对IncrementalStore进行更改),执行无谓词或排序描述符的提取和NSLog获取的实体对象。

一切工作很好,直到我要求任何实体属性。它崩溃与以下消息:

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' 

我的xcdatamodeld设置正确。 NSManagedObject类是在委托上生成和导入的。当我在NSLog之前做一个断点时,我可以看到提取的对象ID。网络服务正在给我返回正确的数据。

我的AppDelegate代码:

- (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); 
} 

如何使这段代码的任何想法回到我所要求的属性?

回答

0

AFNetworking为您提供托管对象ID,即实例NSManagedObjectID。您无法在其上查找托管对象属性值 - 您必须首先获取该ID的托管对象。这就是_NSObjectID_id_0在错误信息中的含义 - 您试图在NSManagedObjectID上获得 eventoId,并且它不知道这是什么。

通过在托管对象上下文中查找来获取托管对象。类似于

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

谢谢汤姆。你是绝对正确的。 –