2013-07-22 39 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

謝謝湯姆。你是絕對正確的。 –