Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27113

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

Upvotes: 0

Views: 77

Answers (2)

Parag Bafna
Parag Bafna

Reputation: 22930

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.

Upvotes: 1

Darshan Kunjadiya
Darshan Kunjadiya

Reputation: 908

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.

Upvotes: 0

Related Questions