drewmccormack / ensembles

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

Question on dedup/seed/merge #196

Closed sarunw closed 9 years ago

sarunw commented 9 years ago

I have 2 questions about ensemble.

First, If my 2 devices got seed data populated on start when it sync the way ensemble handle this from my understanding is it will try to dedup/merge it with - (NSArray *)persistentStoreEnsemble:(CDEPersistentStoreEnsemble *)ensemble globalIdentifiersForManagedObjects:(NSArray *)objects so if my seed data in this case predefine Category i.e. personal/work will get asked for its identifier and if it has the same global id ensemble will dedup and merge it.

So

Device 1
A----Category A

Device 2
B----Category A

will automatically dedup one and both A/B will point to one of Category A for me

Device 1
A----Category A
B----/

Am I right about this ?

Second question is merge, all this merge and dedup is handle magically under the hood I don't sure if I can have finer grain of control about this. Say I have Parent with many Child and every time child was added to parent, parent will have some computation on all of its children and update property on parent.

Say Parent has latestChild property show the latest child based on Child's createdAt property. This value update every time child was add/remove/edit . and Child has createdAt property.

If 2 device create child at the slight diff of time say Device 1 create before Device 2 (createdAt are not the same, user can adjust createdAt date on creation).

Device1
Parent A ----- Child A: createdAt 12:00
- latestChild: Child A

Device2
Parent A ------ Child B: createdAt 11:00
- latestChild: Child B

It would finally be

Parent A ------ Child A/ Child B
- latestChild: Child B (due to the fact the Device 2 create later, so let this property from Device 2 win)

If my assumption was right, can I override this behavior ? I want to recalculate latestChild property before merge.

drewmccormack commented 9 years ago

You are right about how merging works.

To handle computed values, just implement one of the delegate methods, and update the object in the reparation context before it is saved to the store.

sarunw commented 9 years ago

You mean this - (BOOL)persistentStoreEnsemble:(CDEPersistentStoreEnsemble *)ensemble shouldSaveMergedChangesInManagedObjectContext:(NSManagedObjectContext *)savingContext reparationManagedObjectContext:(NSManagedObjectContext *)reparationContext; one right ?

drewmccormack commented 9 years ago

Yep. The book explains in merging chapter.

Kind regards, Drew

On 11 Jan 2015, at 16:46, Sarun Wongpatcharapakorn notifications@github.com wrote:

You mean this - (BOOL)persistentStoreEnsemble:(CDEPersistentStoreEnsemble )ensemble shouldSaveMergedChangesInManagedObjectContext:(NSManagedObjectContext )savingContext reparationManagedObjectContext:(NSManagedObjectContext *)reparationContext; one right ?

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

pronebird commented 8 years ago

@drewmccormack is shouldSaveMergedChangesInManagedObjectContext a preferred delegate method of manual de-duping before saving changes into persistent store? I am not sure what store reperationContext is connected to, can I fetch stuff inside of it and just delete/merge objects that match both in my CoreData and CDE store at the time of merge? (I have objects that change over time so I cannot assign a single unique ID to them.)

drewmccormack commented 8 years ago

You can fetch in the reparation context, yes. It is a child context of the context that is saving.

I think that is indeed to best place to dedupe, but you can also dedupe in the merge completion block.

Kind regards, Drew

On 01 Jul 2016, at 13:15, Andrei Mihailov notifications@github.com wrote:

@drewmccormack https://github.com/drewmccormack is shouldSaveMergedChangesInManagedObjectContext a preferred way of manual de-duping before saving changes into persistent store? I am not sure what store reperationContext is connected to, can I fetch stuff inside of it and just delete/merge objects that match both in my CoreData and CDE store at the time of merge?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/drewmccormack/ensembles/issues/196#issuecomment-229921920, or mute the thread https://github.com/notifications/unsubscribe/AAEuAIA5DWCUZmMhS3aoZOnwX3U5BoPOks5qRPa5gaJpZM4DPtAo.