4

MagicalRecord is a nice library to manage coredata.In my application I have to processes json from web service for managing Json we are using JSONModel. Now the problem is I have to use two separate class to manage magical record and jsonModel.

Is there any way by which I can combine these two? Thanks in Advance.

Eldhose
  • 726
  • 8
  • 19
  • If you are directly importing all data received from web service to core data, you can make use of managed object itself, but you will need to write a few methods yourself such as create or update etc. – Anupdas Jun 21 '13 at 09:06
  • @Anupdas thats by using Magical Record only,isn't? – Eldhose Jun 21 '13 at 09:16
  • 2
    I guess you are aware of that fact that MagicalRecord has some nice import features as such in it. This is a nice [tutorial](http://www.cimgf.com/2012/05/29/importing-data-made-easy/) by the author of MagicalRecord about importing. This will remove the need for having to parse the JSON to someother model object before adding it to core data. – Anupdas Jun 21 '13 at 09:18

1 Answers1

4

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

-(id)mergeWithContext:

Whenever I get a JSON object from the web, 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.

The further in my mergeWithContext: method I just create a new entity matching the current JSONModel object and copy over all values. (I actually also check whether an entity with the model's ID already exists in CoreData - then I update it, otherwise I create a new instance).

Not too difficult and you get a fair amount of flexibility if you need to add some custom behaviour when the data is saved.

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

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73