3lvis / DATAStack

100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer
Other
214 stars 44 forks source link

Background to main context finish notification. #102

Closed AndrewScherba closed 5 years ago

AndrewScherba commented 7 years ago

How I can know when data which was save in background is ready for use in main context (thread)?

3lvis commented 7 years ago

Hi @AndrewScherba,

As soon as you call the context's save method it should be available in the main thread.

func createUser() {
    self.dataStack.performInNewBackgroundContext { backgroundContext in
        let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: backgroundContext)!
        let object = NSManagedObject(entity: entity, insertIntoManagedObjectContext: backgroundContext)
        object.setValue("Background", forKey: "name")
        object.setValue(NSDate(), forKey: "createdDate")
        try! backgroundContext.save()

       // READY IN MAIN THREAD!
    }
}
AndrewScherba commented 7 years ago

emm so all object is sync and ready to use in mains thread after save?? but what about merge and save main context?

3lvis commented 7 years ago

There's an observer for the created background thread: https://github.com/SyncDB/DATAStack/blob/master/Source/DATAStack.swift#L241

So when you save the observer will be called and will merge things in the main thread. https://github.com/SyncDB/DATAStack/blob/master/Source/DATAStack.swift#L351-L361

3lvis commented 5 years ago

Closing until someone else requests this feature.

VitaliyR commented 5 years ago

It would be good to have this feature.

In your example where comment READY is located - the updated object isn't updated in the main thread - only after a short while.