drewmccormack / ensembles

A synchronization framework for Core Data.
MIT License
1.63k stars 132 forks source link

How to monitor root context with ensembles #213

Closed pronebird closed 8 years ago

pronebird commented 8 years ago

Previously I had a monitoring of root context that would normally catch all changes for specific object and index/deindex it from Spotlight based on what's going on with the object. How can I achieve the same with Ensembles when changes arrive from multiple devices and being merged in background?

Would it be enough to just switch notification observer to CDEMonitoredManagedObjectContextDidSaveNotification and CDEMonitoredManagedObjectContextWillSaveNotification?

drewmccormack commented 8 years ago

If you need to change an object before it gets saved, use the delegate methods. Eg persistentStoreEnsemble:shouldSave... See the book for details.

If you mean how do you ignore notifications from the ensembles saves, just check the context and return if it is an unknown context.

Kind regards, Drew

On 01 Jan 2016, at 18:36, Andrey Mikhaylov notifications@github.com wrote:

Previously I had a monitoring of root context that would normally catch all changes for specific object and index/deindex it from my Spotlight index based on what's going on with the object. How can I do the same with Ensembles?

Would it be enough to just switch notification observer to CDEMonitoredManagedObjectContextDidSaveNotification and CDEMonitoredManagedObjectContextWillSaveNotification?

— Reply to this email directly or view it on GitHub.

pronebird commented 8 years ago

Yeah so the thing is, I'd like to pick up added/updated and deleted objects, then update index in Spotlight. So I don't really need to modify objects.

So far I've tried to hook up both will/did save events and CDEMonitoredManagedObjectContextDidSaveNotification is never called. CDEMonitoredManagedObjectContextWillSaveNotification works though.

Maybe I should utilize merge notification from ensembles.

- (void)addObservers { 
    /// ...
    [notificationCenter addObserver:self
                           selector:@selector(ensemblesContextWillSave:)
                               name:CDEMonitoredManagedObjectContextWillSaveNotification
                             object:nil];

    [notificationCenter addObserver:self
                           selector:@selector(ensemblesContextDidSave:)
                               name:CDEMonitoredManagedObjectContextDidSaveNotification
                             object:nil];
}

- (void)ensemblesContextWillSave:(NSNotification *)note {
    [self handleWillSaveNotification:note];
}

- (void)ensemblesContextDidSave:(NSNotification *)note {
    [self handleDidSaveNotification:note];
}

I need willSave / didSave combo because I need to pick up objects that have only specific set of properties changed. This helps to avoid first of all infinite loops of updates (in case if I need to update the relationship on object for example), second, process objects that have relevant information changed. This is kind of information that is impossible to obtain from save or merge notification.

drewmccormack commented 8 years ago

I think you could get that info in the delegate menthods of ensembles. You get passed the context there, and can query for what is changed or about to change. Same goes for the delegate method called just after the save. In fact, these methods are just triggered by the notifications you are observing.

pronebird commented 8 years ago

Thanks @drewmccormack ! I'll give it a shot!

pronebird commented 8 years ago

Yes you were right. It worked. I had to use shouldSaveMergedChangesInManagedObjectContext and didSaveMergeChangesWithNotification to monitor changes. The only side effect I think is if I monitor both Ensembles and my saving context for changes then both are being fired. So maybe I should drop monitoring my own root context and totally rely on Ensembles notifications because merge should always happen with events store, but again I am not sure how it will work in case if iCloud is disabled.