Hello,
I am working on a time table app for students. It will get new data from a webserver and use Core Data to save data locally.
Also I heard that you should use NSManagedContexts only from the thread where you created them.
I will have two objects that access Core Data, the webclient and the UI. The webclient will download the data and save it into Core Data, the UI will only read the saved data. The save and write operations will happen asynchronously.
My plan is the following:
1. Create a class that superviews all Data relevant operations (DataManager).
2. The DataManager has an own private concurrent queue.
3. It also has one NSManagedContext instance.
4. All CoreData related operations will be executed in the DataManager in dispatch_asyc, dispatch_sync (read-operations) and dispatch_barrier (write-operations) closures.
Data downloading procedure:
1. Download the data (background queue)
2. in a dispatch_barrier block that runs on the DataManger queue:
2a. create NSManagedObjects
2b. save the NSManagedContext
Data fetching procedure (with completion handler):
1. in a dispatch_asyc block that runs on the DataManager queue:
1a. if neccessary call the data downloading procedure
1b. fetch the requested objects from the NSManagedContext
1c. call the completion handler
Is it safe to work with Core Data in this manner?
Thanks in advance!