无法检索核心数据实体

时间:2014-03-07 20:38:16

标签: ios objective-c core-data magicalrecord

我正在使用Core DataMagical Record来保存对话对象。在我的viewDidLoad方法中,我想要检索所有的对话。我已尝试过下面的方法1 方法2 (我不会像下面的代码那样同时执行这些操作......我会注释掉一个或另一个)但是似乎正确地检索了对话(_conversations记录一个空数组)

这很奇怪,因为当我在saveObject中保存会话时,我看到我的对象已成功保存(记录成功保存)

为什么会这样?

-viewDidLoad {

   /* Method 1 */
   _conversations = [[Conversation MR_findAllSortedBy:@"createdAt" ascending:YES] mutableCopy];

   /* Method 2 */
   _managedObjectContext = [NSManagedObjectContext MR_defaultContext];

   NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
   NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Conversation" inManagedObjectContext:_managedObjectContext];
   [fetchRequest setEntity:entity];
   NSError *error;
   _conversations = [[_managedObjectContext executeFetchRequest:fetchRequest error:&error]  mutableCopy];
   NSLog(@"conversations: %@",_conversations);
}



-saveObject {
       // Create a new Person in the current thread context
   Conversation *convo                          = [Conversation MR_createInContext:_managedObjectContext];
   convo.name                        = name;
   convo.seen                        = false;
   convo.createdAt                   = createdAt;
   convo.updatedAt                   = updateDate;
   convo.objectid                    = objId;

   // Save the modification in the local context
   // With MagicalRecords 2.0.8 or newer you should use the MR_saveNestedContexts
   [_managedObjectContext MR_saveOnlySelfWithCompletion:^(BOOL Success, NSError *error){
       if (Success)
           NSLog(@"successful save");
       else
           NSLog(@"Error in saving");
}];
}

0 个答案:

没有答案