modo-studio/SugarRecord

Use CoreDataDefaultStorage to write many object as I want.

matrosovDev opened this issue · 5 comments

Hi guys, I have a question regarding bundle. I want to use CoreDataDefaultStorage for all my object to simple write some model to appropriate entity. In the example above seems we just specify one entity that I want to fetch/update/remove.

How can I use entire db to write any objects. I am a bit confused about this parameter.

lazy var db: CoreDataDefaultStorage = {
    let store = CoreData.Store.Named("cd_basic")
    let bundle = NSBundle(forClass: SRCarEntity.classForCoder())
    let model = CoreData.ObjectModel.Merged([bundle])
    let defaultStorage = try! CoreDataDefaultStorage(store: store, model: model)
    return defaultStorage
  }()

Thanks a lot!

Hey @matrosovDev . It's explained in the README:

do {
  db.operation { (context, save) throws -> Void in
    let newTask: Track = try! context.new()
    newTask.name = "Make CoreData easier!"
    try! context.insert(newTask)
    save()
  }
}
catch {
  // There was an error in the operation
}

Let me know if it works for you.

yea this is about isertaion, but my question was what's exactly:

let bundle = NSBundle(forClass: SRCarEntity.classForCoder())
let model = CoreData.ObjectModel.Merged([bundle])

for which purposed we need it and why we just point to one Mode, for example if I want to use many models.

I read it. Not sure for which purposes this line let model = CoreData.ObjectModel.Merged([bundle])

should I create this for all my entities?

Let me correct my self

let bundle = NSBundle(forClass: self.classForCoder())
let model = CoreData.ObjectModel.Merged([bundle])

what does it mean actually self here?

In Apple tutorials it's usually:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

Is this the same?

@matrosovDev what you do with that line is saying:

Take all the .xcdatamodels that you find in the bundle, merge them and then create the store with that merged model.