0
votes

This is a newbie question for core data as I'm starting dev on ios in swift.

I don't understand the mecanism of core data... How can I manipulate, I mean instantiate entities (managed objects) without "attaching" them to the context ?

Let's say I have an api that returns me a list of cars. I want to parse my api data into a list of Car objects. Now I want to exclude all red cars before doing any db operations.

But if I instantiate my cars as managed object they are directly attached to the context, and so if I call save on it, it will do in all my created entities !

Creating a new car object don't mean I want to do any kind of operation with my database !

2
Pls, share us some code you tryed. Also, take a look at raywenderlich.com/145809/getting-started-core-data-tutorial - Jimmy James
Txs for help but your link don't answer my question. It's not a question about code so I dont have any code to post. I want to understand the best practice from the use case I mentioned. - Hiromi
When you parse your JSON object, when you have a red car, you creat a NSManagedObject and save your value into this one. Then, save your context when you have finished to parse the JSON - Jimmy James
Ok so you mean the best practice on ios is to manipulate json before doing any db operations and so create managed objects on the needed values ? Is not really user friendly (even if I agree that creating objects that you should not use is not really "perf") - Hiromi
I would not directly create NSManagedObjects from the JSON Response. I would use a Library like ObjectMapper to create like "in between" objects first. Then filter them. And then map them to NSManagedObjects. - tanaschita

2 Answers

0
votes

ManagedObject that are inserted into a context and then deleted before the context is saved will not effect the database.

  1. Insert managedObject(s) into a context
  2. filter them
  3. Delete the ones you don't want.
  4. Save the context.
0
votes

Managed Objects only exist in contexts. You can't create them in isolation.

As @Jon Rose mentioned, just because you insert them into a context doesn't mean you have to save them. If you delete them again before saving the context, they won't be stored.

You might want to use a separate context for the duration of the parsing operation. You can make a new managed object context, and set it as a child context of your main managed object context (by setting the parentStore property on your newly created managed object context).

The advantage of doing that is that you can guarantee that no other area of your app will be trying to call save: at an inopportune time. In your parser, make the new context and set up its parent. Create all your objects, make all the amendments/deletions you need, and then call save. when you call save on the child managed object context, then and only then are all its changes are pushed up to the parent. (Note that calling save on a child context doesn't actually save things to the database — you'd need to call save on the child context and then call save on the parent context, in that order, in order to save to the database.)