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

  • SEARCH
  • Home
  • 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 8422559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:29:03+00:00 2026-06-10T03:29:03+00:00

I am using multi-threading to get data, parse it, create objects and store them.

  • 0

I am using multi-threading to get data, parse it, create objects and store them. And after this is all done, I want the window to be shown.
But now I have 2 issues:

  • I have a deadlock
  • My barrier does not act as a barrier.

I think the deadlock is because I am updating the managedObjectContext in several threads at once.

  1. So I changed my managedObjectContext with the ConcurrencyType:

    __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    
  2. And created an importContext for the concurrency queue and assigned the parentContext:

    NSManagedObjectContext *importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    importContext.parentContext = self.managedObjectContext;
    
  3. And put my operations in a performBlock for the importContext:

    [importContext performBlock:^{
    
      dispatch_async(backgroundQueue, ^{
      [myObject methodAWithContext:importContext];
      });
    
      dispatch_async(backgroundQueue, ^{
      [myObject methodBWithContext:importContext];
      });
    
      dispatch_async(backgroundQueue, ^{
      [myObject methodCWithContext:importContext];
      });
    
    
      dispatch_barrier_async(backgroundQueueM, ^{
      // create barrier to wait for the first 3 threads to be completed.
       dispatch_async(dispatch_get_main_queue(), ^{
    
        // Save the data from the importContext tot the main context on the main queue
        NSError *importError = nil;
        [importContext save:&importError];
        [importContext.parentContext performBlock:^{
           NSError *parentError = nil;
           [importContext.parentContext save:&parentError];
        }];
    
       [self.window makeKeyAndVisible];
    
       });
      });
    }];
    

Approach 1:
In each method, I select a subset of object, delete these and then create new objects and save this.
(I thought the delete was quicker than doing a fetch and check for the existence for every object to be created).
So:
In Method A I select all AObjects, delete them and create new AObjects.
In Method B I select all BObjects, delete them and create new BObjects.
In Method C I select all CObjects, delete them and create new CObjects.

But then I get an error “An NSManagedObjectContext cannot delete objects in other contexts”.

So approach 2:
I removed the delete. But now I get various different errors…..
And the barrier does not wait for the other threads to be executed.

Q1: What am I doing wrong?

Q2: how do I get the barrier to wait for the 3 threads to be completed

Q3: how can I delete / purge objects on various threads?

(I have read the Apple release notes and doc’s, but I can’t find this a clear explanation on the combination for multithreading and managedContext.)

  • 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-10T03:29:05+00:00Added an answer on June 10, 2026 at 3:29 am

    You cannot call dispatch_async within performBlock. A managed object context of type NSPrivateQueueConcurrencyType has it’s own dispatch queue for executing the operations.

    You try to do several operations in parallel by moving them to a different dispatch queue, but that is not possible.

    If you really have to do multiple operations in parallel, you must create a private concurrency type MOC for each operation.

    ADDED:

    There are several ways to wait for all operations to complete:

    • You could increment a counter at the end of each performBlock: and check if it’s value is (in your example) 3.
    • You could create a semaphore (dispatch_semaphore_create) for each operation with initial value zero, wait for all the semaphores (dispatch_semaphore_wait) and signal the semaphore at the end of each performBlock.
    • And I am sure that there are better/more elegant/more sophisticated ways to do this.

    BUT: As I re-read your question, I see that you try to delay the

    [self.window makeKeyAndVisible];
    

    until all Core Data fetch operations have completed. This is not a good design, because the user will see nothing until your data import is done.

    A better design is to show an initial view immediately, and update that view when the background operations have fetched data.

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

Sidebar

Related Questions

I am using multi threading concept to run some process. this process uses the
I am relatively new to multi-threading and want to execute a background task using
I'm using multi-table-inheritance , and want to know how to create an inherited type
I am using the console application i used multi threading in the same. I
I'm using joda due to it's good reputation regarding multi threading. It goes great
I'm experimenting Java Multi-Threading using synchronization on method comparing with Atomic variables (java.util.concurrent.atomic package).
I'm not the best with multi-threading, and this is a bit complicated, so hopefully
This question is for those of you who, unlike myself, truely understand multi-threading in
this is my first adventure into multi threading and I think I am missing
I need some help in using objects from Core Data with GCD; I seem

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.