我已经设置了两个NSManagedObjectContexts,一个在主线程上,另一个在后台线程上。我目前正在尝试实现NSFetchedResultsController,但似乎主线程的保存通知不包含像后台线程那样的更改。
有关我如何解决这个问题的任何想法?以下是通知的外观(首先记录后台保存,然后是主保存)。
2014-02-28 15:14:24.210 Lift[62931:70b] Notification found with:
name: NSManagingContextDidSaveChangesNotification
object: <NSManagedObjectContext: 0x109280700>
userInfo: {
inserted = "{(\n)}";
updated = "{(\n <Message: 0x109622cc0> (entity: Message; id: 0xd000000000a40006 <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/Message/p41> ; data: {\n \"awaiting_reply\" = 0;\n \"body_excerpt\" = \"\\n\\nOn Friday, February 28, 2014 at 12:17 AM, Cody Robertson wrote:\\n\\n> \\n> \\n> On Friday, February 28, 2014 at 12:16 AM, Cody Robertson wrote:\\n> \\n> > \\n> > \\n> > On Friday, February 28, 2014 at 12:12 AM, Co\";\n cc = \"<relationship fault: 0x109302a10 'cc'>\";\n date = \"2014-02-28 05:19:19 +0000\";\n \"expiration_date\" = nil;\n \"expiration_length\" = 259200;\n folder = \"lift:in\";\n from = \"0xd0000000000c0008 <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/UserInfo/p3>\";\n id = 53101bd032ef5519958b4ecb;\n mailbox = \"0xd000000000040000 <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/Mailbox/p1>\";\n read = 0;\n subject = \"Re: A New Email!\";\n thread = \"0xd00000000060000a <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/Thread/p24>\";\n to = \"<relationship fault: 0x1093002a0 'to'>\";\n}),\n <Message: 0x10975bfd0> (entity: Message; id: 0xd000000000b80006 <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/Message/p46> ; data: {\n \"awaiting_reply\" = 0;\n \"body_excerpt\" = \"\\n\";\n cc = \"<relationship fault: 0x109760580 'cc'>\";\n date = \"2014-02-28 18:33:25 +0000\";\n \"expiration_date\" = nil;\n \"expiration_length\" = 259200;\n folder = \"lift:in\";\n from = \"0xd0000000000c0008 <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/UserInfo/p3>\";\n id = 5310d5eb32ef5519958b4fa7;\n mailbox = \"0xd000000000040000 <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/Mailbox/p1>\";\n read = 0;\n subject = \"New Message\";\n thread = \"0xd0000000007c000a <x-coredata://B1800ECF-053B-417C-81C0-CD304EF83F19/Thread/p31>\";\n to = \"<relationship fault: 0x10975e910 'to'>\";\n}),\n )}";
}
2014-02-28 15:14:24.228 Lift[62931:70b] Notification found with:
name: NSManagingContextDidSaveChangesNotification
object: <NSManagedObjectContext: 0x10927f670>
userInfo: {
inserted = "{(\n)}";
updated = "{(\n)}";
}
这就是我从背景中保存上下文的方式:
- (void)saveFromBackgroundThread:(void(^)(void))completionBlock
{
NSError *error;
if (![_backgroundManagedObjectContext save:&error])
{
NSLog(@"[BACKGROUND_THREAD] - %@", [error localizedDescription]);
}
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
if (![_mainManagedObjectContext save:&error])
{
NSLog(@"[MAIN_THREAD] - %@", [error localizedDescription]);
}
else {
if (completionBlock != nil)
{
completionBlock();
}
}
});
}
答案 0 :(得分:1)
我应该在NSManagedObjectContext类引用的文档中找到你的解决方案,特别是阅读“并发”一节。