Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Discarded Remote Changes (when defaults values are set in core data model) #546

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Simperium/SPChangeProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ - (BOOL)_processRemoteModifyWithKey:(NSString *)simperiumKey
if (oldDiff.count) {
// The local client version changed in the meantime, so transform the diff before applying it
SPLogVerbose(@"Simperium applying transform to diff: %@", diff);
diff = [bucket.differ transform:object diff:oldDiff oldDiff:diff oldGhost:oldGhost error:&theError];
NSDictionary *diffPreferingLocalData = [bucket.differ transform:object diff:oldDiff oldDiff:diff oldGhost:oldGhost error:&theError];

diff = [bucket.differ diffByMergingDiff:diff intoDiff:diffPreferingLocalData overwrite:NO];

if (theError) {
SPLogError(@"Simperium error during diff transform: %@", theError.localizedDescription);
if (error) {
Expand All @@ -325,7 +328,7 @@ - (BOOL)_processRemoteModifyWithKey:(NSString *)simperiumKey
// Load from the ghost data so the subsequent diff is applied to the correct data
// Do an extra check in case there was a problem with the transform/diff, e.g. if a client's own change was misinterpreted
// as another client's change, in other words not properly acknowledged.
if (diff.count) {
if (diffPreferingLocalData.count) {
[object loadMemberData:object.ghost.memberData];
[self enqueueObjectForMoreChanges:simperiumKey bucket:bucket];
} else {
Expand Down
2 changes: 1 addition & 1 deletion Simperium/SPDiffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
- (BOOL)applyDiffFromDictionary:(NSDictionary *)diff toObject:(id<SPDiffable>)object error:(NSError **)error;
- (BOOL)applyGhostDiffFromDictionary:(NSDictionary *)diff toObject:(id<SPDiffable>)object error:(NSError **)error;
- (NSDictionary *)transform:(id<SPDiffable>)object diff:(NSDictionary *)diff oldDiff:(NSDictionary *)oldDiff oldGhost:(SPGhost *)oldGhost error:(NSError **)error;

- (NSDictionary *)diffByMergingDiff:(NSDictionary *)diff2 intoDiff:(NSDictionary *)diff1 overwrite:(BOOL)overwrite;
@end
13 changes: 12 additions & 1 deletion Simperium/SPDiffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ - (NSDictionary *)transform:(id<SPDiffable>)object diff:(NSDictionary *)diff old

if (!ghostValue) {
SPLogError(@"Simperium error: transform diff for a ghost member (ghost %@, memberData %@) that doesn't exist (%@): %@", oldGhost, oldGhost.memberData, key, [change description]);
continue;
}

// Could handle some weird cases here related to dynamically adding/removing members; ignore for now
Expand Down Expand Up @@ -289,4 +288,16 @@ - (NSDictionary *)transform:(id<SPDiffable>)object diff:(NSDictionary *)diff old
return newDiff;
}

- (NSDictionary *)diffByMergingDiff:(NSDictionary *)diff2 intoDiff:(NSDictionary *)diff1 overwrite:(BOOL)overwrite{
//if overwite == NO, then only add value from diff2 if it doesn't exist in diff1
NSMutableDictionary *combinedDiff = [diff1 mutableCopy] ?: [[NSMutableDictionary alloc] init];
for (NSString *key in diff2) {
if (overwrite || (!overwrite && !diff1[key])) {
combinedDiff[key] = diff2[key];
}
}

return [combinedDiff copy];
}

@end