Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone performBlockAndWait:同步保存根托管对象上下文不会';不提供非临时对象ID_Iphone_Objective C_Ios_Core Data_Ios5 - Fatal编程技术网

Iphone performBlockAndWait:同步保存根托管对象上下文不会';不提供非临时对象ID

Iphone performBlockAndWait:同步保存根托管对象上下文不会';不提供非临时对象ID,iphone,objective-c,ios,core-data,ios5,Iphone,Objective C,Ios,Core Data,Ios5,在iOS5发行说明和WWDC2011演示示例之后,我将根托管对象上下文设置为typeNSPrivateQueueConcurrencyType。我有一个子上下文,其类型为NSMainQueueConcurrencyType(称为默认上下文) 当我保存子上下文,然后使用块API保存根上下文时:performBlockAndWait:,我希望操作是同步的 也就是说,在这个块执行之后,我应该能够为所有刚刚插入数据存储的对象获取非临时的ObjectID 但是,我得到了临时对象ID!这就好像perform

在iOS5发行说明和WWDC2011演示示例之后,我将根托管对象上下文设置为typeNSPrivateQueueConcurrencyType。我有一个子上下文,其类型为NSMainQueueConcurrencyType(称为默认上下文)

当我保存子上下文,然后使用块API保存根上下文时:
performBlockAndWait:
,我希望操作是同步的

也就是说,在这个块执行之后,我应该能够为所有刚刚插入数据存储的对象获取非临时的ObjectID

但是,我得到了临时对象ID!这就好像
performBlock和wait:
退化为
performBlock:
并异步运行。但是为什么呢

这是一个错误,还是我遗漏了一些关键假设

以下是一些相关代码:

// Setup of the root MOC:
__rootContext = [[NSManagedObjectContext alloc] 
    initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[__rootContext setPersistentStoreCoordinator:[self coordinator]];

// Setup of the child MOC; I use it as the "default" context:
__defaultContext = [[NSManagedObjectContext alloc] 
    initWithConcurrencyType:NSMainQueueConcurrencyType];
[__defaultContext setParentContext:__rootContext];

// Here's the essence of the save operation:
[__defaultContext save:&error]

// Setup a block I can invoke that does the save:
void (^rootContextSaveOperation)(void) = ^{
    NSError *rootContextError = nil;
    BOOL wasRootContextSaveSuccessful = [rootContext save:&rootContextError];
    if (!wasRootContextSaveSuccessful) { 
        NSLog(@"RPDataStore: Error saving root context."); }
    };

// Call perform block and wait with the operation:
[__rootContext performBlockAndWait:rootContextSaveOperation];

// Now when I inspect one of the objects just saved, I have this check in my unit test:
BOOL isTempID = [[user objectID] isTemporaryID];
问题是,对于MOC层次结构中刚刚从叶MOC保存到根MOC的“user”对象部分,“isTempID”标志不幸地显示为“YES”

我的期望是,我可以同步执行保存,以获得可在其他上下文中使用的永久对象ID

我对
performBlockAndWait:
的概念预期是否不正确


如果是这样的话,我如何与此MOC队列配置同步保存,并且仍然立即获取非临时对象?

对于可能有帮助的人来说,我的解决方法是为将唯一地检索它的托管对象构建一个fetch predict

我对根托管对象上下文执行此提取,然后可靠地获取我以前保存的托管对象的非临时对象ID。在我的例子中,我已经有了托管对象,这些对象报告了在搜索中可以使用哪一组键,这将唯一地标识它们

另一个选项是在初始保存子上下文之前获取上下文中所有对象的永久对象ID。然后,您可以使用NSManagedObjectContext的
refreshObject:mergeChanges:
刷新作用域中特定对象的当前引用。现在,它将反映刚刚保存在子上下文中的更改


使用NSManagedObjectContext的方法获取永久对象ID
获取永久对象:错误:

刚刚在官方开发者论坛上看到,这是一个bug。对于已经注册的用户,请参阅以下帖子:将您的评论移至答案,然后在24小时后接受。否则,这个问题将显示为永远无法回答。谢谢@TechZen-我已将其移到一边,并将按照建议接受。