我正在使用Core Data
和Magical 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");
}];
}