我试图坚持新实体:
- (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);
}];
}
或者像这样:
- (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);
}];
}
但是在完成块中我收到contextDidSave == NO,错误== nil。 所以我无法弄清楚出了什么问题。 我犯了一些明显的错误吗?我该如何调试该问题?
//////
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
更新
以下代码来自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;
}
因此,hasChanges返回NO。
答案 0 :(得分:6)
您的对象在保存块中没有发生任何变化。我在这里看到两个问题。
MR_defaultContext
中创建新对象时,您将在localContext
中创建新对象。试试这个:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createInContext:localContext];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
[Quiz MR_truncateAll]
只需将您的所有Quiz
个对象deleted
属性设置为true
。这意味着下次保存或处理上下文时,将保存更改。
因此,当您创建新的保存上下文时,上下文仍然具有这些对象。我不确定你的fromDictionary
方法在做什么,但如果它依赖于数据库,那么它就不会有它。
你需要这样做:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
[Quiz MR_truncateAllInContext:localContext];
[localContext processPendingChanges];
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createInContext:localContext];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
这样,您将删除保存上下文中的对象。您还必须记住在保存上下文中调用processPendingChanges
以从上下文中删除对象,而不是仅将其标记为要删除。