Martin Koles Martin Koles -3 years ago 132
Objective-C Question

Saving into CoreData Context on background thread

I am struggling with this for some time now and Apple's documentation and SO did not help so far. I was using ManagedObjectContext on a UIManagedDocument and the code below worked fine. I then decided to use Apple's template for CoreData in AppDelegate, so model, persistent store coordinator and context is created in AppDelegate. Fetching with AppDelegate's context is no problem, but background saving is an issue. I should have local context on the thread I am saving and as per Apple to have same persistance store coordinator. But the code below does not actually save the data. Can someone here please advise? Thank you.

- (void)fetchAndPersist
dispatch_queue_t ffetchQ = dispatch_queue_create("ForFetch", NULL);
dispatch_async(ffetchQ, ^{

NSManagedObjectContext *secureManagedObjectContext;
NSPersistentStoreCoordinator *coordinator = [appDelegate persistentStoreCoordinator];
if (coordinator != nil) {
secureManagedObjectContext = [[NSManagedObjectContext alloc] init];
[secureManagedObjectContext setPersistentStoreCoordinator:coordinator];

// find missing date
DataManager *dataManager = [[DataManager alloc] init];
NSDate *missingDate = [dataManager findMissingDateFromDate:selectedDate inContext:secureManagedObjectContext];

if (missingDate) {
// fetch and parse data
DataFetcher *dataFetcher = [[dataFetcher alloc] init];
NSDictionary *fetchResponse = [dataFetcher parseDataForDate:missingDate];

// persist it in a block and wait for it
[secureManagedObjectContext performBlock:^{
DataStore *dataStore = [[DataStore alloc] init];
BOOL parsingError = [dataStore persistData:fetchResponse inContext:secureManagedObjectContext];

if (parsingError) {
// handle error
} else {
dispatch_async(dispatch_get_main_queue(), ^{
// perform on main
[self fetchAndPersist];

Answer Source

Try to use parent / child contexts:

In the link above you can find a code sample.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download