通过MagicalRecord保存NSManagedObject不起作用

问题描述 投票:0回答:2

我创建了一个对象:

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

ios swift core-data magicalrecord
2个回答
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博客以获取完整的演示代码。如果您想了解更多信息,请告诉我。


0
投票

调用MR_saveToPersistentStoreAndWait来保存数据。检查NSManagedObjectContext (MagicalSaves)类中的所有保存方法。

你应该使用MR_createEntityInContext而不是MR_createEntityMR_createEntity已被弃用。

© www.soinside.com 2019 - 2024. All rights reserved.