0

I'm creating an array of NSManagedObject to be used as my UITableView DataSource as follow :

let entity = NSEntityDescription.entityForName("YoutubeAsset", inManagedObjectContext: self.managedContext)!
let asset = YoutubeAsset(entity: entity, insertIntoManagedObjectContext: self.managedContext)
asset.videoId = code
myDataSource.append(asset)

And than, when the users selecting one of the cells, i want to save the specific object into my Core Data Entity.

The problem is that when i'm calling

 do {
        try managedContext.save()
        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        }

It's saves all of my DataSource into Core Data.

How can i "pull" only the selected object out of my managedContext, and save it into Core Data? Much appreciated, Roi!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
  • What are you trying to accomplish? Do you have unsaved changes in the DataSource (aside from the new asset) that you don't want saved? – Lou Franco Jun 28 '16 at 17:58

1 Answers1

1

If the managedContext has no unsaved objects at the beginning of your code sample, then saving the context at the end will result in a single SQL insert.

You cannot save individual entities, just contexts (which save all entities in the context that are unsaved).

If you want to make some changes and have them be unsaved, then you should make those changes in a different context.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Commenting here instead on the question it self - Yes, there are a bunch of entities at the same Context, and i'm trying to save a specific one. Does creating new context for each "single new" object, is the regular practice? or should i break my main context, which holds all the "objects", and than applying the save action? – Roi Mulia Jun 28 '16 at 18:01
  • Normal: Make changes in one context and save unless you have a good reason not to. You can call save after each object is created and keep using the same context. – Lou Franco Jun 28 '16 at 18:08
  • Is it possible if you could attach some sample code, on a scenario which my Context already hold bunch of objects, but i want to save only the first one for example? – Roi Mulia Jun 28 '16 at 18:11
  • I said: "You cannot save individual entities, just contexts", and that contexts save all unsaved entities -- so that sample code is not possible. (yes, you could probably hack something, but that isn't how CoreData is meant to be used). If you want to segregate entities and have them save separately, they need to be in different contexts. – Lou Franco Jun 28 '16 at 18:16
  • AH! lol - my bad, a bit out of focus. Alright, so i need to generate new context for each single new entity i want to save. Does creating new context counted as expensive process? As well, am i creating new context and initializing it via the property from the app delegate, or giving it fresh new value? – Roi Mulia Jun 28 '16 at 18:19
  • Creating contexts is very cheap. However, I would not recommend that you do it in this case. Different conceptual changes should be segregated (like, e.g. the different saves for the results of different API calls returning with data). It doesn't feel like these changes don't all go together -- also, generally, you want to save pretty soon after any insert to get the object a permanent ID. – Lou Franco Jun 28 '16 at 18:21
  • Recommended reading: https://www.objc.io/books/core-data/ or watch this from one of the authors: https://realm.io/news/tryswift-daniel-eggert-modern-core-data/ (and then read the book for details) – Lou Franco Jun 28 '16 at 18:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115870/discussion-between-roi-mulia-and-lou-franco). – Roi Mulia Jun 28 '16 at 18:26