Friday, 23 August 2013

NSManagedObjectControllers read only access on another thread

NSManagedObjectControllers read only access on another thread

I am selecting some Core Data entries and sorting them. This is straight
forward enough but because of the calculations in the sort by distance I
wanted to put it in an alternate thread. I did that the usual way by
making a new NSManagedObjectController for the other thread by adding the
code within the #ifdef THREADS sections below.
Now I can do my fetch, then my sorting and organizing, without slowing the
user interface and the table will be updated when ready.
But, since I am doing my fetch in the local managed object context, my
fetched objects are not valid on my main thread - in fact they disappear
out of my table view cells.
So, my questions are: 1) would it be OK to use the main managed object
context, since I am only reading, and not create a local context at all?
2) should I go through and re-fetch all the objects from the main managed
object context on the main thread using the ObjectID values from the
search managed object context objects after sorting and organizing? 3)
should I do my fetch on the main managed object context with
PerformBlockAndWait but then keep the sort and organize with the returned
objects on the searchQ thread? 4) something else I have not thought of?
- (void) fetchShopLocations {
#ifdef THREADS
dispatch_queue_t searchQ = dispatch_queue_create( "searchQueue", NULL );
dispatch_async( searchQ, ^{

No comments:

Post a Comment