【发布时间】:2017-11-10 03:58:40
【问题描述】:
当我请求新数据时,我想在添加新闻之前删除所有记录。有时记录的旧数据和新数据之间没有变化。
这是我尝试过的,但我的新记录从未保存(总是保存 0 条记录)
当我请求数据时:
Autorisation* aut = [Autorisation MR_createEntity];
// setter method
当我想保存时:
+(void)saveAutorisationList:(NSMutableArray*)autorisationList{
NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];
for (Autorisation* aut in [self getAutorisationList]) {
[aut MR_deleteEntityInContext:localContext]; // method that return all Autorisation
}
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL contextDidSave, NSError * error) {
for (Autorisation* aut in autorisationList) {
[aut MR_inContext:localContext];
}
[localContext MR_saveToPersistentStoreWithCompletion:nil];
}];
}
+(NSMutableArray*)getAutorisationList {
NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];
return [[Autorisation MR_findAllInContext:localContext] mutableCopy];
}
【问题讨论】:
-
我看到您正在删除所有当前记录,但您在哪里创建新记录?
-
在另一个函数中,当我请求一个 HTTP 方法并解析响应时。要创建我使用 [Autorisation MR_createEntity]
-
请分享该代码。
-
我没有什么特别的。如果您看到我是如何创建对象的,那它只是我所做的。之后,我将所有创建的对象添加到一个数组中,并以数组作为参数调用函数
saveAutorisationList。 -
如何将接收到的数据映射到 Autorisation 对象中?
标签: ios core-data magicalrecord