gpt4 book ai didi

ios - 魔法记录,无法保存对象 : contextDidSave == NO, error = nil

转载 作者:可可西里 更新时间:2023-11-01 17:16:01 26 4
gpt4 key购买 nike

我正在尝试持久化新实体:

- (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);
}];
}

但是在完成 block 中我收到 contextDidSave == NO,error == 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。

最佳答案

您的对象在保存 block 中没有发生任何变化。我在这里看到两个问题。

  1. 当您需要在作为保存 block 的保存上下文的 localContext 中创建新对象时,您正在 MR_defaultContext 中创建它们。

试试这个:

- (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);
}];
}
  1. 在您开始导入新对象之前,您的对象实际上并没有被删除。这可能不会导致您的上下文没有任何变化,但我也会讨论这个问题。

[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 以从上下文中删除对象,而不是仅仅将它们标记为要删除。

关于ios - 魔法记录,无法保存对象 : contextDidSave == NO, error = nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com