سؤال

I have setup two NSManagedObjectContexts, one on the main thread and one on a background thread. I am currently trying to implement a NSFetchedResultsController yet it seems the save notifications for the main thread do not contain the changes like it does for the background thread.

Any thoughts on how I can fix this? Here is what the notifications look like (background save logged first, followed by main save).

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)}";
}

This is how I am saving my context from the background:

- (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();
            }
        }
    });
}
هل كانت مفيدة؟

المحلول

I should found your solution in the documentation of NSManagedObjectContext Class reference, especially read the paragraph "Concurrency".

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top