通过MagicalRecord保存NSManagedObject不起作用

时间:2019-04-09 14:25:17

标签: ios swift core-data magicalrecord

我已经创建了一个对象:

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.mr_createEntity

2 个答案:

答案 0 :(得分:0)

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

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博客。 如果需要更多信息,请告诉我。

答案 1 :(得分:0)

致电MR_saveToPersistentStoreAndWait保存数据。检查NSManagedObjectContext (MagicalSaves)类中的所有保存方法。

您应该使用MR_createEntityInContext而不是MR_createEntityMR_createEntity已过时。