MagicalRecord - 检索最后插入的记录ID

时间:2015-10-02 09:25:41

标签: ios objective-c magicalrecord

我正在使用MagicalRecord而我无法理解并检索您刚刚保存的记录的ID

Items *item = [Items MR_createEntity];
item.ref_user = ref_user;
[self saveContext];



- (void)saveContext {
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        if (success) {
            DDLogInfo(@"MR_saveContext success");
            [self loadView];
            [self viewDidLoad];
        } else if (error) {
            DDLogError(@"Error saving context: %@", error.description);
        }
    }];
}

1 个答案:

答案 0 :(得分:1)

为什么不直接创建它?

item = [ITEM MR_createEntityInContext:defaultContext];
[defaultContext MR_saveToPersistentStoreAndWait];

// you can retrieve the id of above item here