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 8693105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:28:06+00:00 2026-06-13T00:28:06+00:00

I have two blocks that are being dispatched concurrently on a user-defined concurrent queue,

  • 0

I have two blocks that are being dispatched concurrently on a user-defined concurrent queue, via GCD. At some point in the blocks flow, they have to access the same set of core data managed objects. Not explicitly through a fetch, but rather through an already fetched object which has a to-many relation set.

As far as I understand, the blocks should each have their own context and then, on completion, merge both contexts to the one in the main thread.

However, I’m wondering if a viable alternative to creating 2 contexts and then merging to a third would be to do something like:

- (void)someMethodThatAppliesToAdisjointSetWithRange:(NSRange)range fromSharedObject:(NSManagedObject*)someSharedObject
{
  //  Do some stuff...

  //  Create sortDescriptors

  __block NSArray* entities = nil;
  dispatch_sync(dispatch_get_main_queue(), ^{
    entities = [[[someSharedObject valueForKey:@"sprites"] sortedArrayUsingDescriptors:sortDescriptors] retain];
    entities = [entities subarrayWithRange:range]
  });

  for(id anEntity in entities)
  {
    //  STRICTLY retrieve properties from the entities
    //  And do stuff with these, BUT DON'T modify the entities themselves.
  }

  //  Continue doing stuff that has nothing to do with the managed objects.
}

- (void)dispatchMethod
{

  dispatch_async(_concurrentQueue, ^{
    [self someMethodThatAppliesToAdisjointSetWithRange:NSMakeRange(0, floor(pageData.count/2.)) fromSharedObject:someSharedObject];
  });

  dispatch_async(_concurrentQueue, ^{
    [self someMethodThatAppliesToAdisjointSetWithRange:NSMakeRange(floor(pageData.count/2.), ceil(pageData.count/2.)) fromSharedObject:someSharedObject];
  });
}

My thought on the above is that the dispatches on the main thread create a synchronization point within the concurrent flow, and since I’m not even modifying the managed objects in themselves, it should be fine. However, at some point throughout a whole day of testing, I got
a core data “statement is still active ” inconsistency exception. So now I’m thinking he above code is still not safe. Though the exception might have actually been caused by other code in the app.

Any thoughts?

  • 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-13T00:28:16+00:00Added an answer on June 13, 2026 at 12:28 am

    The empirical answer is NO you cannot. Presumably because any sort of access to relationships in a managedObject will potentially fault and require the context to implicitly fetch the objects in the relationship anyway.

    That is,

    for(id anEntity in entities)
      {
        //  STRICTLY retrieve properties from the entities
        //  And do stuff with these, BUT DON'T modify the entities themselves.
      }
    

    still requires access to the managedObjectContext.

    The same code using two local contexts instead of the suggested approach does not cause the “Statement is still active” exception.

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

Sidebar

Related Questions

I have two blocks in HTML5 canvas. Blue Block ie fixed in the canvas
I have my main stage, and I have two objects (blocks), these two objects
I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
I have these div blocks called columns where each column div contains separate two
I have a method that is being executed on a background thread. From that
I have two Drupal aggregator blocks on my page, one for a Twitter feed,
I have two inputs reading into my command prompt, the first being a series
In an HTML page, I have three blocks: two textboxes and one inline SVG,
I have a Windows Form application that involves two Forms. The child form is
I have some (php) code that performs critical updates on an InnoDB table. 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.