wibosco / CoreDataServices

CoreDataServices is a suite of helper classes and extensions to help to remove some of the boilerplate that surrounds using Core Data.
https://williamboles.com/
Other
11 stars 2 forks source link

when using `saveBackgroundManagedObjectContext`, when is update available? #13

Open haemi opened 7 years ago

haemi commented 7 years ago

e.g.

DispatchQueue.global(qos: .background).async { [weak self] in
    ServiceManager.sharedInstance.backgroundManagedObjectContext.performAndWait({
        let user = NSEntityDescription.insertNewObject(entityClass: CDUserModel.self, managedObjectContext: ServiceManager.sharedInstance.backgroundManagedObjectContext)
        user.name = self?.nameEntry.textField.text

        ServiceManager.sharedInstance.saveBackgroundManagedObjectContext()

        DispatchQueue.main.async(execute: {
            let userModel = ServiceManager.sharedInstance.mainManagedObjectContext.retrieveFirstEntry(entityClass: CDUserModel.self)
            print(userModel?.name)
        })
    })
}

here, the print(userModel?.name) is not updated... am I missing something?

wibosco commented 7 years ago

@haemi Sorry about the delay in getting back, somehow I didn't see this issue.

In the above example is there only one CDUserModel i.e. the retrieveFirstEntry call isn't pulling a different instance of CDUserModel out?

haemi commented 7 years ago

@wibosco same here, didn't see this, sorry (weird)

not, there's only one CDUserModel...

wibosco commented 7 years ago

That is odd, based on your code snippet I would expect the CDUserModel instance to have the name value assigned on the background context.

I don't suppose this is an open source project that you could link to allow me to debug it?