简体   繁体   English

通过MagicalRecord保存NSManagedObject不起作用

[英]Saving NSManagedObject via MagicalRecord does not work

I have create an object: 我创建了一个对象:

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
}

}

My question is how to save ExerciseEntity.mr_createEntity correctly 我的问题是如何正确保存ExerciseEntity.mr_createEntity

Call MR_saveToPersistentStoreAndWait to save data. 调用MR_saveToPersistentStoreAndWait保存数据。 Check all save methods in NSManagedObjectContext (MagicalSaves) class. 检查NSManagedObjectContext (MagicalSaves)类中的所有保存方法。

You should use MR_createEntityInContext instead of MR_createEntity . 您应该使用MR_createEntityInContext而不是MR_createEntity MR_createEntity is deprecated. 不推荐使用MR_createEntity

Please try below code to store entity. 请尝试以下代码存储实体。

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()

Do not forgot to add below line in AppDelegate file. 不要忘记在AppDelegate文件中添加以下行。

MagicalRecord.setupAutoMigratingCoreDataStack()

See this raywenderlich blog for full demo code with steps. 请参阅此raywenderlich博客,以获取具有步骤的完整演示代码。 Do let me know if you want more information. 如果您需要更多信息,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM