使用MagicalImport导入数据后如何保存上下文?

时间:2013-07-16 00:35:01

标签: ios core-data magicalrecord magicalrecord-2.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];

//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

知道我做错了吗?

2 个答案:

答案 0 :(得分: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];
}];

答案 1 :(得分:1)

[[NSManagedObjectContext MR_defaultContext]saveToPersistentStoreAndWait];