dispatch_async上下文的任务问题

时间:2014-04-09 12:54:36

标签: ios multithreading core-data nsmanagedobject nsmanagedobjectcontext

我正在使用dispatch_async任务来检索包含某些联系人的列表 从此列表中选择联系人时,我致电Storage Manager以检索此联系人的其他一些信息 我收到错误: 在不同环境中的对象之间非法尝试建立关系 .... 怎么解决这个问题?
这是我的代码:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
      sections = [self getContactList];
   });

informationList = [StorageManager getInfoForContact:[sections objectAtIndex:1]];

错误位于informationList的行。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我不全面了解你的问题。但是,您将[self getContactList]调用为异步块。因此,当[self getContactList]正在执行时,将调用块外的下一行。这意味着:

informationList = [StorageManager getInfoForContact:[sections objectAtIndex:1]];

在执行之前执行:

sections = [self getContactList];

可能你的错误就在那里。

尝试:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    sections = [self getContactList];
    dispatch_sync(dispatch_get_main_queue(), ^{
        informationList = [StorageManager getInfoForContact:[sections objectAtIndex:1]];
    });
});