1

我使用 MagicalImport(2.1 版)MR_importFromArray 方法导入了一个 json 数组。我正在尝试使用 MR_findAll 检索导入的 NSManagedObjects,但没有任何成功。以下是我迄今为止尝试过的变体:

//Attempt 1
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
  [Thread MR_importFromArray:threadsData];
} completion:^(BOOL success, NSError *error) {
  NSArray *allThreads = [Thread MR_findAll];
}];

//Attempt 2
[MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *localContext) {
  [Thread MR_importFromArray:threadsData inContext:localContext];
} completion:^(BOOL success, NSError *error) {
  NSArray *allThreads = [Thread MR_findAllInContext:[NSManagedObjectContext MR_contextForCurrentThread]];
}];

//Attempt 3
NSManagedObjectContext *context =[NSManagedObjectContext MR_defaultContext];
 [Thread MR_importFromArray:threadsData inContext:context];
[context MR_saveToPersistentStoreAndWait];
NSArray *allThreads = [Thread MR_findAllInContext:context];

//Attempt 4
[Thread MR_importFromArray:threadsData];
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
NSArray *allThreads = [Thread MR_findAll];

这是我在控制台中看到的错误:

-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0xb2566e0) NO CHANGES IN ** DEFAULT ** CONTEXT - NOT SAVING

知道我做错了什么吗?

4

2 回答 2

1
[[NSManagedObjectContext MR_defaultContext]saveToPersistentStoreAndWait];
于 2014-03-20T15:26:53.287 回答
1

找到了答案。似乎 MR_importFromArray 中有一个错误。当我手动迭代对象并将它们保存到上下文时,它工作正常。就是这样 :

[MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *localContext) {
  [threadsData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    [Thread MR_importFromObject:obj];
  }];
} completion:^(BOOL success, NSError *error) {
  NSArray *allThreads = [Thread MR_findAll];
}];
于 2013-07-16T00:56:15.813 回答