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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:13:34+00:00 2026-05-28T14:13:34+00:00

I have a multi-threaded app that uses Core Data. I’ve been seeing a lot

  • 0

I have a multi-threaded app that uses Core Data. I’ve been seeing a lot of crashes on startup, and various bizarre error messages. However, sometimes it works fine! I have never seen a crash on my own iPhone4, but it does crash on other devices. I think I’ve figured out the issue, but am not sure how to best solve it.

When the app starts, I use GCD to download data from the web and update core data on a background thread. On the main thread, I continue with setting up table views, and one of these commands triggers the NSManagedObjectContext getter. I’m using pretty much the standard template code, so as it is the first time this is run, the usual lazy instanciation code creates the NSPersistentStoreCoordinator.

However the background thread is creating a new NSManagedObjectContext for it’s own use. This includes getting the NSPersistentStoreCoordinator as follows:

    // Create a new NSManagedObjectContext for this thread
    NSManagedObjectContext *threadContext = nil;
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        threadContext = [[NSManagedObjectContext alloc] init];
        [threadContext setPersistentStoreCoordinator:coordinator];
        NSMergePolicy *mergePolicy = [[NSMergePolicy alloc] initWithMergeType:NSMergeByPropertyObjectTrumpMergePolicyType];
        [threadContext setMergePolicy:mergePolicy];
    } else {
        NSLog(@"Error - No NSPersistentStoreCoordinator");
    }

I’m 90% sure that issue is that both threads are getting to the NSPersistentStoreCoordinator code at the same time. Both threads believe the object is nil, therefore create the object. This causes issues for the first thread to get there as the ivar suddenly points to the wrong place. At that point bad things happen!

So how best to solve this? I have temporarily fixed the issue by adding a sleep(1) to the background thread 🙂 but I’m not sure this is really the best solution! I have tried changing the NSPersistentStoreCoordinator properties so that they are atomic, but this hasn’t helped. Should I wrap each of the standard getters in @synchronise to mutex lock them? I haven’t had time to try this yet, but it should stop the same code being run by both threads. Or is there a better way?

Thoughts?

Thanks Craig

======================================

For info, here are some of the errors and crashes I have seen. These are pretty random, but it may help others find this post in the future.

2012-01-18 22:19:57.586 CBF[10468:11d03] -[NSSQLModel
_addPersistentStore:identifier:]: unrecognized selector sent to instance 0x6b80390

2012-01-18 22:19:57.595 CBF[10468:11d03] * Terminating app due to
uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSSQLModel
_addPersistentStore:identifier:]: unrecognized selector sent to instance 0x6b80390’

2012-01-19 16:58:06.671 CBF[11738:fe03] -[__NSCFDictionary
_hasPrecomputedKeyOrder]: unrecognized selector sent to instance 0x6d55040

2012-01-25 21:45:31.174 CBF[16911:1310b] -[__NSArrayM
_addPersistentStore:identifier:]: unrecognized selector sent to instance 0x6d59370

2012-01-25 21:45:31.175 CBF[16911:1310b] * Terminating app due to
uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSArrayM
_addPersistentStore:identifier:]: unrecognized selector sent to instance 0x6d59370’

  • 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-05-28T14:13:35+00:00Added an answer on May 28, 2026 at 2:13 pm

    Why not just remove the lazy instantiation, and instead create the coordinator immediately when the app starts?

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

Sidebar

Related Questions

I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to
I have got a multi-threaded app that process a very large data file. Works
I have a multi threaded JAVA app that uses a shared c lib. What
I have a multi threaded app that writes log to file. occasionaly, saving fails
I have a multi-threaded Cocoa app that processes images. The program has a progress
I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library.
I have a multi-threaded Windows C++ app written in Visual Studio 6. Within the
I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
If I have a multi-threaded program that reads a cache-type memory by reference. Can
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering

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.