Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:36:27+00:00 2026-06-08T08:36:27+00:00

Since two days I’m trying to get Core Data to work with multiple threads.

  • 0

Since two days I’m trying to get Core Data to work with multiple threads. I tried standard thread confinement method with NSOperations, merging notifications, using objectWithId, dictionaries of contexts per thread and still I get strange deadlocks, inconsistency exceptions and a bunch of other nasty stuff. It’s driving me crazy… moreover I can’t find a single example or explanation on how to manage context in two threads when both threads may make changes to the shared persistent store…

I tried to use new iOS 5 method, that supposed to be easier, but still I get errors. The first problem is the deadlock when saving context. I removed all the unnecessary code and stil get deadlocks when executing this code fast enough (by quickly tapping a button):

    NSManagedObjectContext *context = [StoreDataRetriever sharedRetriever].managedObjectContext;

    for (int i = 0; i < 5; i++) {            
        NSError *error = nil;
        NSLog(@"Main thread: %@, is main? %d", [NSThread currentThread], [NSThread isMainThread]);
        BOOL saveOK = [context save:&error];

        if (!saveOK) {
            NSLog(@"ERROR!!! SAVING CONTEXT IN MAIN");
        }

        [context performBlock:^{

            NSLog(@"Block thread: %@", [NSThread currentThread]);

            NSError *error = nil;
            BOOL savedOK = NO;

            savedOK = [context save:&error]; 

            if (!savedOK) {
                NSLog(@"ERROR!!! SAVING CONTEXT IN BLOCK");
            }
        }];
    }

There are no other changes to the database, nothing, only saving context. What is wrong with this code? How should it look like?

Note: [StoreDataRetriever sharedRetriever].managedObjectContext is created in appDelegate using initWithConcurrencyType:NSPrivateQueueConcurrencyType.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:36:29+00:00Added an answer on June 8, 2026 at 8:36 am

    What’s going on with that code? You are saving the context on a thread synchronously, then you schedule a save on the context private queue. 5 times. So basically, you may well have two save operations, one synchronous and one asynchronous, colliding with each other.

    This is clearly an issue. You aren’t supposed to save a context with a private queue outside of that queue. It will work with the current context implementation provided there is no scheduled block on the context queue. But this is wrong nevertheless.

    …
    for (int i = 0; i < 5; i++) {            
        NSLog(@"Main thread: %@, is main? %d", [NSThread currentThread], [NSThread isMainThread]);
        __block NSError *error = nil;
        __block BOOL saveOK = YES;
    [context performBlockAndWait: ^{
        saveOK = [context save: &error];
    }];
    
        if (!saveOK) {
            NSLog(@"ERROR!!!");
        }
        …
    

    With that code, you execute the save operation synchronously and most certainly on the same thread – thanks GCD – sparing context switches and synchronization stuff, and without any risk of having two operations running on that context at the same time.

    The same rule applies when using NSMainQueueConcurrencyType, with an exception. That queue is bound to the main thread and the main thread only. You can schedule blocks on a context using the main queue from any thread with performBlock and performBlockAndWait like NSPrivateQueueConcurrencyType, and (the exception:) you can use the context directly on the main thread.

    NSConfinementConcurrencyType binds the context to a specific thread and you cannot use GCD or blocks to deal with such a context, only the bound thread. There is very little reasons to use that concurrency model as of today. If you have to, use it, but if you do not absolutely have to, don’t.


    edit

    Here is a very nice article about multi-contextes setups: http://www.cocoanetics.com/2012/07/multi-context-coredata/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying since two days to install OpenCV 2.3.0 or 2.3.1 on my
Since two days I am trying to consume a WCF (.NET) Soap Service and
I have a serious problem since two days trying to rewrite the urls of
I have been trying since yesterday to get google like paging, but each time
[Major Edit based on experience since 1st post two days ago.] I am building
Since two days I am playing with C and I come across new problem
I'm using Apache Maven3 and since two or three days some dependencies can't be
note: using django/python/javascript/flash So its been two days since I'm stuck at the error.
I'm currently trying to count user activity, but will only count it two days
Since I updated two days ago my Android SDK to version API 14 I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.