代码之家  ›  专栏  ›  技术社区  ›  Oleksandr Matrosov

通过magicalrecord保存nsmanagedobject不起作用

  •  0
  • Oleksandr Matrosov  · 技术社区  · 5 年前

    我创建了一个对象:

    let exercise = ExerciseEntity.mr_createEntity()
    
    func updateLocalExercise(with exercise: ExerciseEntity, completion: @escaping (ResultInfo<[ExerciseEntity]>) -> Void) {
    
                    MagicalRecord.save({ (context) in
    
    if let localExercise = exercise.mr_(context) { this actually nil and throws some error in log
    }
    
    }
    

    我的问题是如何正确保存exerciseentity.

    1 回复  |  直到 5 年前
        1
  •  1
  •   Parag Bafna    5 年前

    呼叫 MR_saveToPersistentStoreAndWait 以保存数据。检查中的所有保存方法 NSManagedObjectContext (MagicalSaves) 班级。

    你应该用 MR_createEntityInContext 而不是 MR_createEntity . _CreateEntity先生 已弃用。

        2
  •  0
  •   Darshan Kunjadiya    5 年前

    请尝试以下代码来存储实体。

    let compltedTaskModel : CompletedTaskModel = CompletedTaskModel.mr_createEntity()!
     compltedTaskModel.title = taskList.title
     compltedTaskModel.description_text = taskList.description_text
     compltedTaskModel.workflow_description = taskList.workflow_description
     compltedTaskModel.last_updated = taskList.last_updated
     compltedTaskModel.respondent_pk = taskList.respondent_pk
     compltedTaskModel.isFlag = taskList.isFlag
     NSManagedObjectContext.mr_default().mr_saveToPersistentStoreAndWait()
    

    不要忘记在appdelegate文件中添加下面的行。

    MagicalRecord.setupAutoMigratingCoreDataStack()
    

    this raywenderlich 博客完整的演示代码与步骤。 如果你想知道更多的信息,一定要告诉我。