Roi Mulia Roi Mulia - 1 year ago 68
Swift Question

Issue understanding Core Data fundamentals

I'm creating an array of

to be used as my
as follow :

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

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 {
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")

It's saves all of my
into Core Data.

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download