MR_inContext 为零

Nil for MR_inContext

根据本教程 (https://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Working-with-Managed-Object-Contexts.md),我尝试找到我的设备并对其进行更新。

Person *person = ...;

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext){

  Person *localPerson = [person MR_inContext:localContext];
  localPerson.firstName = @"John";
  localPerson.lastName = @"Appleseed";

} completion:^(BOOL success, NSError *error) {

  self.everyoneInTheDepartment = [Person findAll];

}];

所以我做了:

CDDevice *device = [CDDevice MR_findFirstByAttribute:@"deviceName"
                       withValue:uniqueName];

它找到了我的设备。在我测试设备是否具有正确的会话和授权代码的几个 IF 语句之后,我想更新它。

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext){
                         CDDevice * localDevice = [device MR_inContext:localContext];
                         [localDevice updateFromDictionary:messageDictionary];
                 } completion:^(BOOL success, NSError *error) {
                         NET_LOG(@"Updating current device %@", device);
                 }];

但我的 localDevice 始终为零。是因为 MR_findFirstByAttribute 运行 在不同的上下文中吗?更新我的设备的正确方法是什么?

所有这些都发生在我的自定义串行队列上,因为这段代码在项目的网络部分。 (使用 GCDAsyncUdpSocket 的接收方法)

确保在后台上下文中检索数据之前保存数据。