JSONModel with MagicalRecord

MagicalRecord is a good library for managing coredata. In my application, I have to process json from a web service to control Json, which we use JSONModel . Now the problem is that I have to use two separate classes to control the magic record and jsonModel.

Is there a way I can combine these two? Thank you in advance.

+4
source share
1 answer

What I personally do is add a method to all JSONModel instances:

- (ID) mergeWithContext:

Whenever I get a JSON object from the Internet, the JSONModel parses it for me and converts the data to what I need, then if I want to save it to CoreData, I just call mergeWithContext: and pass the current context to it.

Further in my mergeWithContext method: I just create a new object corresponding to the current JSONModel and copy all the values. (In fact, I also check if the object with the model identifier exists in CoreData - then I update it, otherwise I will create a new instance).

Not too complicated, and you get enough flexibility if you need to add some kind of custom behavior when saving data.

mergeWithContext: returns, of course, the object itself, so I can work with it further if I need to.

+4
source

Source: https://habr.com/ru/post/1487435/


All Articles