4
votes

I have two threads operating in my app.

  • In the Main thread I update values of keys in entities or gets some rows from Core Data.
  • In the background thread I download data from a server.

But some times during update/processing on core 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 does the NSManagedObjectContext get locked? What can I do for this?

2
Please accept appropriate answers to your previous questions, this will likely get you more good answers here. - tilo
I also had the same issue. Do you fix it? - Naveen Shan
Yes .Make sure same managedobject context isn't accessed on multiple thread at a time.Please look over accepted answer - Ritika

2 Answers

3
votes

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

Most likely, you have a problem managing the context on the separate threads. Make sure you use a different context for each thread and make sure you don't pass managed objects between the threads. Make sure you merge the contexts before trying to access changes made on one thread from another.

1
votes

Mastering CoreData in multithreaded apps can be difficult. Make sure you create a new NSManagedObjectContext for each thread that uses CoreData. The MOC must be created in the thread where it is used. The MOC itself is not thread safe. CoreData does not lock it for you. If you use a new MOC for each thread you don't have to do the locking. In your stacktrace you can see that the NSPersistentStoreCoordinator is locked (not the MOC). CoreData does this so that only one MOC can access the NSPersistentStoreCoordinator at the same time.