Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Fix an issue with synchronization of empty values
Browse files Browse the repository at this point in the history
Empty values would be dropped early during parsing responses under
some circumstances, leading to the issue that emptying a value in
Contentful would not update a local persistent data store.

This is now fixed by ensuring empty values will always be present
as part of the `fields` dictionary.
  • Loading branch information
neonichu committed May 13, 2016
1 parent 9d0e04b commit 72be3fd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
6 changes: 4 additions & 2 deletions Code/CDAEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ -(NSString *)locale {
}

-(NSDictionary *)localizedDictionaryFromDictionary:(NSDictionary *)dictionary
forLocale:(NSString *)locale {
forLocale:(NSString *)locale
default:(BOOL)isDefault {
NSDictionary* localizedDictionary = [super localizedDictionaryFromDictionary:dictionary
forLocale:locale];
forLocale:locale
default:isDefault];
return [self parseDictionary:localizedDictionary];
}

Expand Down
4 changes: 3 additions & 1 deletion Code/CDAResource+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
client:(CDAClient*)client
localizationAvailable:(BOOL)localizationAvailable;
-(NSDictionary*)localizeFieldsFromDictionary:(NSDictionary*)fields;
-(NSDictionary*)localizedDictionaryFromDictionary:(NSDictionary*)dictionary forLocale:(NSString*)locale;
-(NSDictionary*)localizedDictionaryFromDictionary:(NSDictionary*)dictionary
forLocale:(NSString*)locale
default:(BOOL)isDefault;
-(NSDictionary*)parseDictionary:(NSDictionary*)dictionary;
-(void)resolveLinksWithIncludedAssets:(NSDictionary*)assets entries:(NSDictionary*)entries;
-(BOOL)updatedAfterDate:(NSDate*)date;
Expand Down
13 changes: 8 additions & 5 deletions Code/CDAResource.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ -(NSDictionary*)localizeFieldsFromDictionary:(NSDictionary*)fields {
NSMutableDictionary* localizedFields = [@{} mutableCopy];

if (self.localizationAvailable) {
NSDictionary* defaultDictionary = [self localizedDictionaryFromDictionary:fields forLocale:self.defaultLocaleOfSpace];
NSDictionary* defaultDictionary = [self localizedDictionaryFromDictionary:fields forLocale:self.defaultLocaleOfSpace default:YES];
localizedFields[self.defaultLocaleOfSpace] = defaultDictionary;

for (NSString* locale in self.client.space.localeCodes) {
Expand All @@ -237,7 +237,8 @@ -(NSDictionary*)localizeFieldsFromDictionary:(NSDictionary*)fields {
}

NSDictionary* localizedDictionary = [self localizedDictionaryFromDictionary:fields
forLocale:locale];
forLocale:locale
default:NO];

localizedFields[locale] = [[CDAFallbackDictionary alloc] initWithDictionary:localizedDictionary fallbackDictionary:defaultDictionary];
}
Expand All @@ -248,7 +249,9 @@ -(NSDictionary*)localizeFieldsFromDictionary:(NSDictionary*)fields {
return [localizedFields copy];
}

-(NSDictionary*)localizedDictionaryFromDictionary:(NSDictionary*)dictionary forLocale:(NSString*)locale {
-(NSDictionary*)localizedDictionaryFromDictionary:(NSDictionary*)dictionary
forLocale:(NSString*)locale
default:(BOOL)isDefault {
NSParameterAssert(dictionary);
NSParameterAssert(locale);

Expand All @@ -257,11 +260,11 @@ -(NSDictionary*)localizedDictionaryFromDictionary:(NSDictionary*)dictionary forL
[dictionary enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSDictionary* value, BOOL *stop) {
id localizedValue = value[locale];

if (!localizedValue) {
if (!localizedValue && !isDefault) {
return;
}

result[key] = localizedValue;
result[key] = localizedValue ?: [NSNull null];
}];

return [result copy];
Expand Down
1 change: 1 addition & 0 deletions Tests/PersistenceBaseTest+Basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ -(void)basic_syncEmptyField {
NSArray* entries = [self.persistenceManager fetchEntriesFromDataStore];
XCTAssertEqual(1U, entries.count, @"");
id updatedEntry = [entries firstObject];
XCTAssertNotNil(updatedEntry);
XCTAssertNil([updatedEntry valueForKey:@"name"]);

EndBlock();
Expand Down
2 changes: 1 addition & 1 deletion Tests/SyncTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ -(void)testSyncEmptyField {
[space performSynchronizationWithSuccess:^{
XCTAssertEqual(1U, space.entries.count, @"");
CDAEntry* updatedEntry = space.entries[0];
XCTAssertNil(updatedEntry.fields[@"test"]);
XCTAssertEqualObjects(updatedEntry.fields[@"test"], @"");

EndBlock();
} failure:^(CDAResponse *response, NSError *error) {
Expand Down

0 comments on commit 72be3fd

Please sign in to comment.