【问题标题】:How to purge an entity and after add new records with magical record如何清除实体并在添加带有神奇记录的新记录后
【发布时间】: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


【解决方案1】:

这里发生的是您正在删除所有对象,包括您要保存的对象。这是一步一步发生的事情:

+(void)saveAutorisationList:(NSMutableArray*)autorisationList {
    // as it seems, here autorisationList is a list of new objects you want to save.

    NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];

错误的行为从这里开始:

    for (Autorisation* aut in [self getAutorisationList]) {

这里getAutorisationList 获取了所有对象,当前存在于localContext,旧的+新的。

      [aut MR_deleteEntityInContext:localContext];  
      // here you deleted each Autorisation object currently existing, including those you want to save
    }
    ...
}

相反,您应该找到您收到的内容与之前存在的内容之间的差异,并仅删除那些未随更新收到的对象。

例如假设您有一组对象OldSet = {auth1, auth2, auth3},并且通过更新您收到了对象NewSet = {auth2, auth3, auth4}。 删除的差异将是

ToBeDeletedSet = OldSet - NewSet = {auth1}

这样,您将保留已有的记录,并保存新的记录。

然后,您的保存方法将如下所示:

+(void)saveAutorisationList:(NSMutableArray*)updatedAutorisationList{
    NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];
    NSMutableArray *oldAutorisationList = [self getAutorisationList];
    [oldAutorisationList removeObjectsInArray: updatedAutorisationList];
    for (Autorisation* aut in oldAutorisationList) {
      [aut MR_deleteEntityInContext:localContext];  // method that return all Autorisation
    }
    [localContext MR_saveToPersistentStoreWithCompletion:^(BOOL contextDidSave, NSError * error) {
        for (Autorisation* aut in updatedAutorisationList) {
            [aut MR_inContext:localContext];
        }
        [localContext MR_saveToPersistentStoreWithCompletion:nil];
    }];
}

【讨论】:

  • 这是终极解决方案。感谢您的澄清
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
相关资源
最近更新 更多