I have a NSManagedObjectContext stacked like this:
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil )
{
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
NSManagedObjectContext* masterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
masterContext.undoManager = nil;
[masterContext setPersistentStoreCoordinator:coordinator];
NSManagedObjectContext* mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
mainContext.parentContext = masterContext;
mainContext.undoManager = nil;
NSManagedObjectContext* workerContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
workerContext.parentContext = mainContext;
workerContext.undoManager = nil;
_managedObjectContext = workerContext;
}
return _managedObjectContext;
}
I am using this context everywhere to read/write on objects of type NSManagedObject.
Here is the save method which is recursive in nature and saves the changes
+ (void) saveManagedObjectContext:(NSManagedObjectContext *)managedObjectContext withCompletion:(void (^)(BOOL, NSError *))completion
{
NSLog(#"\nout current queue: %#",[NSOperationQueue currentQueue]);
[managedObjectContext performBlock:^{
NSLog(#"\ninside current queue: %# current thread: %#",[NSOperationQueue currentQueue],[NSThread currentThread]);
NSError *error = nil;
if (![managedObjectContext save:&error]){
completion(NO, error);
} else {
if ([managedObjectContext parentContext] != nil){
[self saveManagedObjectContext:[managedObjectContext parentContext] withCompletion:completion];
} else {
completion(YES, error);
}
}
}];
}
But my app UI gets freeze every time the saveManagedObjectContext: method is called.
Moreover the NSLog statements gives unusual output:
out current queue: <NSOperationQueue: 0x15c527680>{name = 'NSOperationQueue Main Queue'}
inside current queue: (null) current thread: <NSThread: 0x15c6a86d0>{number = 4, name = (null)}
Why current queue is null?
Why my app is freezing or main thread is being blocked ?
I am using CoreData in my app and I have two main entities, let's say Cats and Dogs.
I have this method that I use to initialise my context:
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:#selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
It refers to this method in my App Delegate:
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
Now, at some point, I have to update some data in my Cats entity.
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Cats"];
NSMutableArray *catsArrayData = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSManagedObject *aCat = [catsArrayData objectAtIndex:i];
NSMutableArray *catsArray = [aCat valueForKey:#"cat"];
[catsArray addObject:responseData];
[aCat setValue:catsArray forKey:#"cat"];
NSError *error = nil;
[context save:&error];
Then, I want to do the same for my Dogs entity. So I use the same code, but for the dogs entity. It's in another part of my code, so I have to redefine:
NSManagedObjectContext *context = [self managedObjectContext];
But the problem is that even though I save this second context, it is actually not saved. The data isn't there. How can I resolve this issue?
You need implement your own change notification mechanism, This is good source to learn it. Multi-Context CoreData.
I'm getting the error "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Context already has a coordinator; cannot replace.'" while executing my app.
In the DataBaseManager.m I have the following code :
AppDelegate *sharedDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *privateWriterContext = [sharedDelegate privateWriterContext];
NSManagedObjectContext *context = [sharedDelegate managedObjectContextChild];
context.parentContext = privateWriterContext;
NSManagedObjectContext *contextforThread = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
contextforThread.parentContext = context;
[contextforThread performBlock:^{
//things to do in background
NSError *error;
if (![contextforThread save:&error])
{
// handle error
}
[context performBlock:^{
NSError *error;
if (![context save:&error])
{
// handle error
}
[privateWriterContext performBlock:^{
NSError *error;
if (![privateWriterContext save:&error])
{
// handle error
}
}];
}];
}];
and in appDelegate.m I have the following code :
- (NSManagedObjectContext *)managedObjectContextChild
{
if (_managedObjectContextChild != nil) {
return _managedObjectContextChild;
}
_managedObjectContextChild = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
return _managedObjectContextChild;
}
- (NSManagedObjectContext *)privateWriterContext
{
if (_privateWriterContext != nil) {
return _privateWriterContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_privateWriterContext setPersistentStoreCoordinator:coordinator];
}
return _privateWriterContext;
}
Xcode shows the line
context.parentContext = privateWriterContext;
when error occurs.
How could I rectify this error?
At the top of - (NSManagedObjectContext *)managedObjectContextChild put a breakpoint and see where else that NSManagedObjectContext is being used.
The error is telling you that it already has a persistent store coordinator, so that's your line of attack.
You are accessing these as single instances so the other areas of the application need to know what to do with them (and what not to do!). You could also inspect persistentStoreCoordinator and parentContext property just before the error to help get a picture of what's happening.
It's nothing terrible and shouldn't be too difficult to find with a couple of sensibly placed breakpoints, however you may have to think about how you manage the various contexts more carefully to solve the issue.
I have a singleton class that saves JSON objects to a CoreData database in the background using GCD (Grand Central Dispatch) queues. This works perfectly the majority of the time, but on iPad 2 and iPad Mini devices I am experiencing some issues with the process freezing.
My setup is pretty straight forward. I have a background dispatch queue (backgroundQueue) that is set to run serially, and I have a seperate instance of NSManagedObjectContext for the background queue. When I want to save something to the database I call the method that begins the save process, and In that method I use dispatch_async to invoke my save logic on the background thread.
Once all processing logic has ran I save the background MOC, and use NSManagedObjectContextDidSaveNotification to merge the changes from the background MOC to the main thread MOC. The background thread waits for this to complete, and once it's done it'll run the next block in the queue, if any. Below is my code.
dispatch_queue_t backgroundQueue;
- (id)init
{
if (self = [super init])
{
// init the background queue
backgroundQueue = dispatch_queue_create(VS_CORE_DATA_MANAGER_BACKGROUND_QUEUE_NAME, DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)saveJsonObjects:(NSDictionary *)jsonDict
objectMapping:(VS_ObjectMapping *)objectMapping
class:(__unsafe_unretained Class)managedObjectClass
completion:(void (^)(NSArray *objects, NSError *error))completion
{
[VS_Log logDebug:#"**** Queue Save JSON Objects for Class: %#", managedObjectClass];
// process in the background queue
dispatch_async(backgroundQueue, ^(void)
{
[VS_Log logDebug:#"**** Dispatch Save JSON Objects for Class: %#", managedObjectClass];
// create a new process object and add it to the dictionary
currentRequest = [[VS_CoreDataRequest alloc] init];
// set the thead name
NSThread *currentThread = [NSThread currentThread];
[currentThread setName:VS_CORE_DATA_MANAGER_BACKGROUND_THREAD_NAME];
// if there is not already a background context, then create one
if (!_backgroundQueueManagedObjectContext)
{
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
// create the background queue
_backgroundQueueManagedObjectContext = [[NSManagedObjectContext alloc] init];
[_backgroundQueueManagedObjectContext setPersistentStoreCoordinator:coordinator];
[_backgroundQueueManagedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
_backgroundQueueManagedObjectContext.undoManager = nil;
// listen for the merge changes from context did save notification on the background queue
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(mergeChangesFromBackground:) name:NSManagedObjectContextDidSaveNotification object:_backgroundQueueManagedObjectContext];
}
}
// save the JSON dictionary
NSArray *objects = [self saveJSON:jsonDict objectMapping:objectMapping class:managedObjectClass managedObjectContext:_backgroundQueueManagedObjectContext level:0];
// save the objects so we can access them later to be re-fetched and returned on the main thread
if (objects.count > 0)
[currentRequest.objects addObjectsFromArray:objects];
// save the object IDs and the completion block to global variables so we can access them after the save
if (completion)
currentRequest.completionBlock = completion;
[VS_Log logDebug:#"**** Save MOC for Class: %#", managedObjectClass];
// save all changes object context
[self saveManagedObjectContext];
});
}
- (void)mergeChangesFromBackground:(NSNotification *)notification
{
[VS_Log logDebug:#"**** Merge Changes From Background"];
// save the current request to a local var since we're about to be processing it on the main thread
__block VS_CoreDataRequest *request = (VS_CoreDataRequest *)[currentRequest copy];
__block NSNotification *bNotification = (NSNotification *)[notification copy];
// clear out the request
currentRequest = nil;
dispatch_sync(dispatch_get_main_queue(), ^(void)
{
[VS_Log logDebug:#"**** Start Merge Changes On Main Thread"];
// merge changes to the primary context, and wait for the action to complete on the main thread
[_managedObjectContext mergeChangesFromContextDidSaveNotification:bNotification];
NSMutableArray *objects = [[NSMutableArray alloc] init];
// iterate through the updated objects and find them in the main thread MOC
for (NSManagedObject *object in request.objects)
{
NSError *error;
NSManagedObject *obj = [[self managedObjectContext] existingObjectWithID:object.objectID error:&error];
if (error)
[self logError:error];
if (obj)
[objects addObject:obj];
}
// call the completion block
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(objects, nil);
}
[VS_Log logDebug:#"**** Complete Merge Changes On Main Thread"];
// clear the request
request = nil;
});
[VS_Log logDebug:#"**** Complete Merge Changes From Background"];
}
When the issue occurs everything seems to run fine, and it gets into the mergeChangesFromBackground: method, but the dispatch_async() is not invoked.
As I mentioned above the code runs perfectly in most cases. Only time it has issues running is when I am testing on either an iPad 2 or iPad Mini and saving large objects.
Does anyone out there have any ideas why this is happening?
Thanks
** EDIT **
Since writing this I've learned of nested NSManagedObjectContexts. I have modified my code to use it and it seems to be working well. However, I still have the same issues. Thoughts? Also, any comments on nested MOCs?
- (void)saveJsonObjects:(NSDictionary *)jsonDict
objectMapping:(VS_ObjectMapping *)objectMapping
class:(__unsafe_unretained Class)managedObjectClass
completion:(void (^)(NSArray *objects, NSError *error))completion
{
// process in the background queue
dispatch_async(backgroundQueue, ^(void)
{
// create a new process object and add it to the dictionary
__block VS_CoreDataRequest *currentRequest = [[VS_CoreDataRequest alloc] init];
// set the thead name
NSThread *currentThread = [NSThread currentThread];
[currentThread setName:VS_CORE_DATA_MANAGER_BACKGROUND_THREAD_NAME];
// if there is not already a background context, then create one
if (!_backgroundQueueManagedObjectContext)
{
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
// create the background queue
_backgroundQueueManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_backgroundQueueManagedObjectContext setParentContext:[self managedObjectContext]];
[_backgroundQueueManagedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
_backgroundQueueManagedObjectContext.undoManager = nil;
}
}
// save the JSON dictionary starting at the upper most level of the key path
NSArray *objects = [self saveJSON:jsonDict objectMapping:objectMapping class:managedObjectClass managedObjectContext:_backgroundQueueManagedObjectContext level:0];
// if no objects were processed, then return with an error
if (!objects || objects.count == 0)
{
currentRequest = nil;
// dispatch the completion block
dispatch_async(dispatch_get_main_queue(), ^(void)
{
NSError *error = [self createErrorWithErrorCode:100 description:#"No objects were found for the object mapping"];
[self logMessage:error.debugDescription];
completion(nil, error);
});
}
// save the objects so we can access them later to be re-fetched and returned on the main thread
if (objects.count > 0)
[currentRequest.objects addObjectsFromArray:objects];
// save the object IDs and the completion block to global variables so we can access them after the save
if (completion)
currentRequest.completionBlock = completion;
// save all changes object context
NSError *error = nil;
[_backgroundQueueManagedObjectContext save:&error];
if (error)
[VS_Log logError:error.localizedDescription];
dispatch_sync(dispatch_get_main_queue(), ^(void)
{
NSMutableArray *objects = [[NSMutableArray alloc] init];
// iterate through the updated objects and find them in the main thread MOC
for (NSManagedObject *object in currentRequest.objects)
{
NSError *error;
NSManagedObject *obj = [[self managedObjectContext] existingObjectWithID:object.objectID error:&error];
if (error)
[self logError:error];
if (obj)
[objects addObject:obj];
}
// call the completion block
if (currentRequest.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = currentRequest.completionBlock;
saveCompletionBlock(objects, nil);
}
});
// clear out the request
currentRequest = nil;
});
}
** EDIT **
Hi Everyone,
I'd first like to thank everyone for this input on this, as that is what lead me to finding the solution for my problem. Long story short I did away completely with using the GCD and the custom background queue, and instead am now using nested contexts and the "performBlock" method to perform all saving on the background thread. This is working great and I elevated my hang up issue.
However, I now have a new bug. Whenever I run my app for the first time, whenever I try to save an object that has child object relations, I get the following exception.
-_referenceData64 only defined for abstract class. Define -[NSTemporaryObjectID_default _referenceData64]!
Below is my new code.
- (void)saveJsonObjects:(NSDictionary *)jsonDict
objectMapping:(VS_ObjectMapping *)objectMapping
class:(__unsafe_unretained Class)managedObjectClass
completion:(void (^)(NSArray *objects, NSError *error))completion
{
[_backgroundManagedObjectContext performBlock:^(void)
{
// create a new process object and add it to the dictionary
VS_CoreDataRequest *currentRequest = [[VS_CoreDataRequest alloc] init];
currentRequest.managedObjectClass = managedObjectClass;
// save the JSON dictionary starting at the upper most level of the key path
NSArray *objects = [self saveJSON:jsonDict objectMapping:objectMapping class:managedObjectClass managedObjectContext:_backgroundManagedObjectContext level:0];
if (objects.count == 0)
{
currentRequest.error = [self createErrorWithErrorCode:100 description:#"No objects were found for the object mapping"];
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidFail:) withObject:currentRequest waitUntilDone:NO];
}
else
{
// save the objects so we can access them later to be re-fetched and returned on the main thread
[currentRequest.objects addObjectsFromArray:objects];
// save the object IDs and the completion block to global variables so we can access them after the save
currentRequest.completionBlock = completion;
[_backgroundManagedObjectContext lock];
#try
{
[_backgroundManagedObjectContext processPendingChanges];
[_backgroundManagedObjectContext save:nil];
}
#catch (NSException *exception)
{
currentRequest.error = [self createErrorWithErrorCode:100 description:exception.reason];
}
[_backgroundManagedObjectContext unlock];
// complete the process on the main thread
if (currentRequest.error)
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidFail:) withObject:currentRequest waitUntilDone:NO];
else
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidSucceed:) withObject:currentRequest waitUntilDone:NO];
// clear out the request
currentRequest = nil;
}
}];
}
- (void)backgroundSaveProcessDidFail:(VS_CoreDataRequest *)request
{
if (request.error)
[self logError:request.error];
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(nil, request.error);
}
}
- (void)backgroundSaveProcessDidSucceed:(VS_CoreDataRequest *)request
{
// get objects from main thread
NSArray *objects = nil;
if (request.objects.count > 0)
{
NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:[NSString stringWithFormat:#"%#", request.managedObjectClass]];
fetchReq.predicate = [NSPredicate predicateWithFormat:#"self IN %#", request.objects];
objects = [self executeFetchRequest:fetchReq];
}
// call the completion block
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(objects, nil);
}
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
// create the MOC for the backgroumd thread
_backgroundManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_backgroundManagedObjectContext setPersistentStoreCoordinator:coordinator];
[_backgroundManagedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
_backgroundManagedObjectContext.undoManager = nil;
// create the MOC for the main thread
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setParentContext:_backgroundManagedObjectContext];
[_managedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
}
return _managedObjectContext;
}
Do any of you have any idea why this crash is happening?
You might want to consider this kind of context architecture (untested code):
The mainManagedObjectContext will be initialised in the same manner but with NSMainQueueConcurrencyType and no observer (by the way, remember to remove your observer)
- (void) mergeChangesFromBackground:(NSNotification*)notification
{
__block __weak NSManagedObjectContext* context = self.managedObjectContext;
[context performBlockAndWait:^{
[context mergeChangesFromContextDidSaveNotification:notification];
}];
}
- (NSManagedObjectContext*) bgContext
{
if (_bgContext) {
return _bgContext;
}
NSPersistentStoreCoordinator* coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_bgContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_bgContext setPersistentStoreCoordinator:coordinator];
[_bgContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
[_bgContext setUndoManager:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(mergeChangesFromBackground:)
name:NSManagedObjectContextDidSaveNotification
object:_bgContext];
return _bgContext;
}
- (void) doSomethingOnBGContextWithoutBlocking:(void(^)(NSManagedObjectContext*))contextBlock
{
__block __weak NSManagedObjectContext* context = [self bgContext];
[context performBlock:^{
NSThread *currentThread = [NSThread currentThread];
NSString* prevName = [currentThread name];
[currentThread setName:#"BGContextThread"];
contextBlock(context);
[context reset];
[currentThread setName:prevName];
}];
}
I'd first like to thank everyone for this input on this, as that is what lead me to finding the solution for my problem. Long story short I did away completely with using the GCD and the custom background queue, and instead am now using nested contexts and the "performBlock" method to perform all saving on the background thread. This is working great and I elevated my hang up issue.
However, I now have a new bug. Whenever I run my app for the first time, whenever I try to save an object that has child object relations, I get the following exception.
-_referenceData64 only defined for abstract class. Define -[NSTemporaryObjectID_default _referenceData64]!
Below is my new code.
- (void)saveJsonObjects:(NSDictionary *)jsonDict
objectMapping:(VS_ObjectMapping *)objectMapping
class:(__unsafe_unretained Class)managedObjectClass
completion:(void (^)(NSArray *objects, NSError *error))completion
{
[_backgroundManagedObjectContext performBlock:^(void)
{
// create a new process object and add it to the dictionary
VS_CoreDataRequest *currentRequest = [[VS_CoreDataRequest alloc] init];
currentRequest.managedObjectClass = managedObjectClass;
// save the JSON dictionary starting at the upper most level of the key path
NSArray *objects = [self saveJSON:jsonDict objectMapping:objectMapping class:managedObjectClass managedObjectContext:_backgroundManagedObjectContext level:0];
if (objects.count == 0)
{
currentRequest.error = [self createErrorWithErrorCode:100 description:#"No objects were found for the object mapping"];
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidFail:) withObject:currentRequest waitUntilDone:NO];
}
else
{
// save the objects so we can access them later to be re-fetched and returned on the main thread
[currentRequest.objects addObjectsFromArray:objects];
// save the object IDs and the completion block to global variables so we can access them after the save
currentRequest.completionBlock = completion;
[_backgroundManagedObjectContext lock];
#try
{
[_backgroundManagedObjectContext processPendingChanges];
[_backgroundManagedObjectContext save:nil];
}
#catch (NSException *exception)
{
currentRequest.error = [self createErrorWithErrorCode:100 description:exception.reason];
}
[_backgroundManagedObjectContext unlock];
// complete the process on the main thread
if (currentRequest.error)
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidFail:) withObject:currentRequest waitUntilDone:NO];
else
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidSucceed:) withObject:currentRequest waitUntilDone:NO];
// clear out the request
currentRequest = nil;
}
}];
}
- (void)backgroundSaveProcessDidFail:(VS_CoreDataRequest *)request
{
if (request.error)
[self logError:request.error];
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(nil, request.error);
}
}
- (void)backgroundSaveProcessDidSucceed:(VS_CoreDataRequest *)request
{
// get objects from main thread
NSArray *objects = nil;
if (request.objects.count > 0)
{
NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:[NSString stringWithFormat:#"%#", request.managedObjectClass]];
fetchReq.predicate = [NSPredicate predicateWithFormat:#"self IN %#", request.objects];
objects = [self executeFetchRequest:fetchReq];
}
// call the completion block
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(objects, nil);
}
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
// create the MOC for the backgroumd thread
_backgroundManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_backgroundManagedObjectContext setPersistentStoreCoordinator:coordinator];
[_backgroundManagedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
_backgroundManagedObjectContext.undoManager = nil;
// create the MOC for the main thread
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setParentContext:_backgroundManagedObjectContext];
[_managedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
}
return _managedObjectContext;
}
Do any of you have any idea why this crash is happening?
** EDIT **
Ladies and gents I have found the solution. Apparently, even when using nested contexts you still have to merge the changes via the NSManagedObjectContextDidSaveNotification. Once I added that into my code it all started to work perfectly. Below is my working code. Thanks a million guys!
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil)
return _managedObjectContext;
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
// create the MOC for the backgroumd thread
_backgroundManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_backgroundManagedObjectContext setPersistentStoreCoordinator:coordinator];
[_backgroundManagedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
_backgroundManagedObjectContext.undoManager = nil;
// create the MOC for the main thread
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setParentContext:_backgroundManagedObjectContext];
[_managedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(mergeChangesFromContextDidSaveNotification:) name:NSManagedObjectContextDidSaveNotification object:_backgroundManagedObjectContext];
}
return _managedObjectContext;
}
- (void)saveJsonObjects:(NSDictionary *)jsonDict
objectMapping:(VS_ObjectMapping *)objectMapping
class:(__unsafe_unretained Class)managedObjectClass
completion:(void (^)(NSArray *objects, NSError *error))completion
{
// perform save on background thread
[_backgroundManagedObjectContext performBlock:^(void)
{
// create a new process object and add it to the dictionary
VS_CoreDataRequest *currentRequest = [[VS_CoreDataRequest alloc] init];
currentRequest.managedObjectClass = managedObjectClass;
// save the JSON dictionary starting at the upper most level of the key path
NSArray *objects = [self saveJSON:jsonDict objectMapping:objectMapping class:managedObjectClass managedObjectContext:_backgroundManagedObjectContext level:0];
if (objects.count == 0)
{
currentRequest.error = [self createErrorWithErrorCode:100 description:#"No objects were found for the object mapping"];
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidFail:) withObject:currentRequest waitUntilDone:NO];
}
else
{
// save the objects so we can access them later to be re-fetched and returned on the main thread
[currentRequest.objects addObjectsFromArray:objects];
// save the object IDs and the completion block to global variables so we can access them after the save
currentRequest.completionBlock = completion;
[_backgroundManagedObjectContext lock];
#try
{
[_backgroundManagedObjectContext processPendingChanges];
[_backgroundManagedObjectContext save:nil];
}
#catch (NSException *exception)
{
currentRequest.error = [self createErrorWithErrorCode:100 description:exception.reason];
}
[_backgroundManagedObjectContext unlock];
// complete the process on the main thread
if (currentRequest.error)
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidFail:) withObject:currentRequest waitUntilDone:NO];
else
[self performSelectorOnMainThread:#selector(backgroundSaveProcessDidSucceed:) withObject:currentRequest waitUntilDone:NO];
// clear out the request
currentRequest = nil;
}
}];
}
- (void)mergeChangesFromContextDidSaveNotification:(NSNotification *)notification
{
[_managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}
- (void)backgroundSaveProcessDidFail:(VS_CoreDataRequest *)request
{
if (request.error)
[self logError:request.error];
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(nil, request.error);
}
}
- (void)backgroundSaveProcessDidSucceed:(VS_CoreDataRequest *)request
{
// get objects from main thread
NSArray *objects = nil;
if (request.objects.count > 0)
{
NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:[NSString stringWithFormat:#"%#", request.managedObjectClass]];
fetchReq.predicate = [NSPredicate predicateWithFormat:#"self IN %#", request.objects];
objects = [self executeFetchRequest:fetchReq];
}
// call the completion block
if (request.completionBlock)
{
void (^saveCompletionBlock)(NSArray *, NSError *) = request.completionBlock;
saveCompletionBlock(objects, nil);
}
}
Our table view controllers use an NSFetchedResultsController to show data from Core Data. We download new data in the background. When an entity is modified in the new data, on iOS 5.1.1 phone, we see that treated as a new row in the table instead of an update. Cannot duplicate on the iOS 5.1 simulator or an iOS 6 device.
The UIApplicationDelegate creates a NSManagedObjectContext with concurrency type NSMainQueueConcurrencyType. Our UITableViewController implements NSFetchedResultsControllerDelegate. In viewWillAppear we go fetch new data. In the method getting data, we create a second NSManagedObjectContext with concurrenty Type NSPrivateQueueConcurrencyType. We do a performBlock on that new context, and do the network call and json parsing. There's a NSFetchRequest to get the previous data, so we can delete the old objects, or modify any existing entities with the same id. After modify the existing entity or creating new ones, we then deleteObject the old entity objects. Then we save this private context. Then on the parent context, do a performBlock to save the changes there.
On iOS5.1, the table is incorrect. If we change on of the objects, instead of being modified, it is added to the table as a new row. If we leave this controller and come back to it, getting new data, it shows the right amount.
AppDelegate.m
- (void)saveContext
{
[self.privateWriterContext performBlock:^{
NSError *error = nil;
[self.privateWriterContext save:&error];
// Handle error...
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:self.privateWriterContext];
}];
}
#pragma mark - Core Data stack
- (NSManagedObjectContext *)privateWriterContext
{
if (__privateWriterContext != nil) {
return __privateWriterContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[__privateWriterContext setPersistentStoreCoordinator:coordinator];
}
return __privateWriterContext;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[__managedObjectContext setParentContext:self.privateWriterContext];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(saveContext:)
name:NSManagedObjectContextDidSaveNotification
object:__managedObjectContext];
return __managedObjectContext;
}
class that fetches from server
+ (void) fetchFromURL:(NSString *) notificationsUrl withManagedObjectContext (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
importContext.parentContext = managedObjectContext;
[importContext performBlock: ^{
NSError *error;
NSURLResponse *response;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSData *responseData = [NSData dataWithContentsOfURLUsingCurrentUser:[NSURL URLWithString:notificationsUrl] returningResponse:&response error:&error];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSMutableSet *newKeys = [[NSMutableSet alloc] init];
NSArray *notifications;
if(responseData) {
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSMutableDictionary *previousNotifications = [[NSMutableDictionary alloc] init];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Notification"];
NSArray * oldObjects = [importContext executeFetchRequest:request error:&error];
for (Notification* oldObject in oldObjects) {
[previousNotifications setObject:oldObject forKey:oldObject.notificationId];
}
notifications = [json objectForKey:#"notifications"];
//create/update objects
for(NSDictionary *notificationDictionary in notifications) {
NSString *notificationId = [notificationDictionary objectForKey:#"id"];
Notification *notification = [previousNotifications objectForKey:notificationId];
if(notification) {
[previousNotifications removeObjectForKey:notificationId];
} else {
notification = [NSEntityDescription insertNewObjectForEntityForName:#"Notification" inManagedObjectContext:importContext];
[newKeys addObject:notificationId];
}
notification.notificationId = [notificationDictionary objectForKey:#"id"];
//other properties from the json response
}
for (NSManagedObject * oldObject in [previousNotifications allValues]) {
[importContext deleteObject:oldObject];
}
}
if (![importContext save:&error]) {
NSLog(#"Could not save to main context after update to notifications: %#", [error userInfo]);
}
//persist to store and update fetched result controllers
[importContext.parentContext performBlock:^{
NSError *parentError = nil;
if(![importContext.parentContext save:&parentError]) {
NSLog(#"Could not save to store after update to notifications: %#", [error userInfo]);
}
}];
}
];
}
I suffered the problem recently too.
The problem is because of two contexts in different threads.
On device which is running iOS 5.1, merging them cause it to insert a new record instead of update it. I change the background thread to use main context instead and the problem is gone.
No clue why merging does not work in this particular case.