【问题标题】:AFIncrementalStore simple fetch terminating with a crashAFIncrementalStore 简单获取以崩溃终止
【发布时间】:2013-07-22 19:58:20
【问题描述】:

我正在使用 AFIncrementalStore 设置一个非常简单的 NSIncrementalStore 示例。

我们的想法是在 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);
}

关于如何使这段代码返回我要求的属性的任何想法?

【问题讨论】:

    标签: ios core-data afincrementalstore


    【解决方案1】:

    AFNetworking 为您提供托管对象 ID,即 NSManagedObjectID 的实例。您不能在上面查找托管对象的属性值——您必须首先获取 ID 的托管对象。这就是 _NSObjectID_id_0 在错误消息中的意思——你试图得到 eventoId 在 NSManagedObjectID 上,它不知道那是什么。

    您可以通过在托管对象上下文中查找托管对象来获取它。类似的东西

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2019-03-27
      • 2018-03-16
      相关资源
      最近更新 更多