NSManagedObjectContext Locked

I have two threads working in my application.

  • In the main thread, I update the key values ​​in the entity or get some rows from Core Data.
  • In the background thread, I am loading data from the server.

But several times when updating / processing master data on [managedObjectContext executeFetchRequest:request error:&error]

... I get:

#0 0x34507c5c in semaphore_wait_signal_trap ()
#1 0x34507f58 in semaphore_wait_signal ()

 #2 0x364d573a in pthread_mutex_lock () #3 0x35c91a2e in -[_PFLock lock] () #4 0x35c91a12 in -[NSPersistentStoreCoordinator lock] () #5 0x35c919e8 in -[NSManagedObjectContext(_NSInternalAdditions) lockObjectStore] () #6 0x35c90676 in -[NSManagedObjectContext executeFetchRequest:error:] () 

How is NSManagedObjectContext blocked? What can I do for this?

+4
source share
2 answers

Locking is part of normal operation and is not the source of your problem.

Most likely, you have a problem with managing the context in separate threads. Make sure that a different context is used for each thread, and make sure that you are not passing managed objects between threads. Make sure you merge contexts before trying to access changes made in one thread from another.

+3
source

Mastering CoreData in multi-threaded applications can be difficult. Make sure you create a new NSManagedObjectContext for each thread that uses CoreData. MOC must be created in the stream where it is used. MOC itself is not thread safe. CoreData does not block it for you. If you use a new MOC for each thread, you do not need to do a lock. In your stack, you can see that the NSPsistentStoreCoordinator is blocked (and not MOC). CoreData does this so that only one MOC can access the NSPsistentStoreCoordinator at a time.

+1
source

All Articles