简体   繁体   中英

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

Call MR_saveToPersistentStoreAndWait to save data. Check all save methods in NSManagedObjectContext (MagicalSaves) class.

You should use MR_createEntityInContext instead of MR_createEntity . MR_createEntity is deprecated.

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.

MagicalRecord.setupAutoMigratingCoreDataStack()

See this raywenderlich blog for full demo code with steps. Do let me know if you want more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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