I'm trying to persist new entities:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[Quiz MR_truncateAll];
[[NSManagedObjectContext MR_context] MR_saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createEntity];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
Or like this:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[Quiz MR_truncateAll];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createEntity];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
But in completion block I receive contextDidSave == NO, error == nil. So I cannot figure out what went wrong. Are there some obvious mistakes I made? How can I debug that issue?
//////
2015-06-17 20:39:27.061 HITO[6733:618953] Set root saving context: <NSManagedObjectContext: 0x16dbe070>
2015-06-17 20:39:27.062 HITO[6733:618953] Created new main queue context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.063 HITO[6733:618953] Set default context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.316 HITO[6733:618953] [HockeySDK] WARNING: Detecting crashes is NOT enabled due to running the app with a debugger attached.
2015-06-17 20:39:28.829 HITO[6733:618953] Created new private queue context: <NSManagedObjectContext: 0x16d57870>
2015-06-17 20:39:28.831 HITO[6733:619027] Created new private queue context: <NSManagedObjectContext: 0x16ea4ec0>
2015-06-17 20:39:28.841 HITO[6733:619027] NO CHANGES IN ** saveWithBlock:completion: ** CONTEXT - NOT SAVING
update
Following code from MR:
- (void) MR_saveWithOptions:(MRSaveOptions)saveOptions completion:(MRSaveCompletionHandler)completion;
{
__block BOOL hasChanges = NO;
if ([self concurrencyType] == NSConfinementConcurrencyType)
{
hasChanges = [self hasChanges];
}
else
{
[self performBlockAndWait:^{
hasChanges = [self hasChanges];
}];
}
if (!hasChanges)
{
MRLogVerbose(@"NO CHANGES IN ** %@ ** CONTEXT - NOT SAVING", [self MR_workingName]);
if (completion)
{
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, nil);
});
}
return;
}
So, hasChanges returns NO.
Your objects have no changes occurring in the save block. There are two problems I see here.
MR_defaultContext
when you need to be creating them in thelocalContext
that is the saving context for your save block.Try this:
[Quiz MR_truncateAll]
just sets all of yourQuiz
objectsdeleted
property totrue
. This means the next time that the context is saved or processed, the changes will be saved.So, when you create your new saving context that context still has those objects. I'm not sure what your
fromDictionary
method is doing, but if its relying on an database, then it's not going to have it.You need to do it like this:
This way, you are deleting the objects in the saving context. You have to also remember to call
processPendingChanges
on the saving context to delete the objects from the context, rather than just marking them to be deleted.