Skip to content

Commit

Permalink
Added support for transformed values in the entity model.
Browse files Browse the repository at this point in the history
Previously this was not supported. If there is a value transformer
associated, it is now applied to the value before adding to the sync
change, or retrieving it.
  • Loading branch information
drewmccormack committed Aug 6, 2012
1 parent 9c2be33 commit e57262e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Expand Up @@ -760,7 +760,7 @@ - (void)applyObjectInsertedSyncChange:(TICDSSyncChange *)aSyncChange

NSString *entityName = [aSyncChange objectEntityName];
NSString *ticdsSyncID = aSyncChange.objectSyncID;
NSManagedObject *object = nil;
TICDSSynchronizedManagedObject *object = nil;

// Check to see if the object already exists before inserting it.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
Expand All @@ -782,7 +782,9 @@ - (void)applyObjectInsertedSyncChange:(TICDSSyncChange *)aSyncChange
NSArray *changedAttributeKeys = [[aSyncChange changedAttributes] allKeys];
for (id key in changedAttributeKeys) {
[object willChangeValueForKey:key];
[object setPrimitiveValue:[[aSyncChange changedAttributes] valueForKey:key] forKey:key];
id transformedValue = [[aSyncChange changedAttributes] valueForKey:key];
transformedValue = [object reverseTransformedValueOfAttribute:key withValue:transformedValue];
[object setPrimitiveValue:transformedValue forKey:key];
[object didChangeValueForKey:key];
}

Expand All @@ -796,7 +798,7 @@ - (void)applyAttributeChangeSyncChange:(TICDSSyncChange *)aSyncChange
{
TICDSLog(TICDSLogVerbosityEveryStep, @"Applying Attribute Change sync change");

NSManagedObject *object = [self backgroundApplicationContextObjectForEntityName:[aSyncChange objectEntityName] syncIdentifier:[aSyncChange objectSyncID]];
TICDSSynchronizedManagedObject *object = (id)[self backgroundApplicationContextObjectForEntityName:[aSyncChange objectEntityName] syncIdentifier:[aSyncChange objectSyncID]];

if( !object ) {
TICDSLog(TICDSLogVerbosityErrorsOnly, @"Object not found locally for attribute change [%@] %@", aSyncChange, [aSyncChange objectEntityName]);
Expand All @@ -807,7 +809,9 @@ - (void)applyAttributeChangeSyncChange:(TICDSSyncChange *)aSyncChange
TICDSLog(TICDSLogVerbosityManagedObjectOutput, @"[%@] %@", aSyncChange, [aSyncChange objectEntityName]);

[object willChangeValueForKey:[aSyncChange relevantKey]];
[object setPrimitiveValue:[aSyncChange changedAttributes] forKey:[aSyncChange relevantKey]];
id transformedValue = [aSyncChange changedAttributes];
transformedValue = [object reverseTransformedValueOfAttribute:[aSyncChange relevantKey] withValue:transformedValue];
[object setPrimitiveValue:transformedValue forKey:[aSyncChange relevantKey]];
[object didChangeValueForKey:[aSyncChange relevantKey]];

TICDSLog(TICDSLogVerbosityManagedObjectOutput, @"Changed attribute on object: %@", object);
Expand Down
Expand Up @@ -28,4 +28,7 @@
@property (nonatomic, readonly) NSManagedObjectContext *syncChangesMOC;
@property (nonatomic, readwrite, assign) BOOL excludeFromSync;

- (id)transformedValueOfAttribute:(NSString *)key;
- (id)reverseTransformedValueOfAttribute:(NSString *)key withValue:(id)value;

@end
31 changes: 26 additions & 5 deletions TICoreDataSync/02 Primary Classes/TICDSSynchronizedManagedObject.m
Expand Up @@ -67,9 +67,7 @@ - (void)createSyncChangesForChangedProperties
TICDSLog(TICDSLogVerbosityManagedObjectOutput, @"Not creating a change for %@.%@", [self class], eachPropertyName);
continue;
}

id eachValue = [changedValues valueForKey:eachPropertyName];


NSRelationshipDescription *relationship = [[[self entity] relationshipsByName] valueForKey:eachPropertyName];
NSAttributeDescription *attribute = [[[self entity] attributesByName] valueForKey:eachPropertyName];
if ( relationship && !relationship.isTransient ) {
Expand All @@ -79,6 +77,7 @@ - (void)createSyncChangesForChangedProperties
TICDSSyncChange *syncChange = [self createSyncChangeForChangeType:TICDSSyncChangeTypeAttributeChanged];
TICDSLog(TICDSLogVerbosityManagedObjectOutput, @"[%@] %@", syncChange.objectSyncID, [self class]);
[syncChange setRelevantKey:eachPropertyName];
id eachValue = [self transformedValueOfAttribute:eachPropertyName];
[syncChange setChangedAttributes:eachValue];
}
}
Expand Down Expand Up @@ -216,14 +215,36 @@ - (void)createToManyRelationshipSyncChanges:(NSRelationshipDescription *)aRelati
}

#pragma mark -
#pragma mark Dictionaries
#pragma mark Attributes
- (id)transformedValueOfAttribute:(NSString *)key
{
NSAttributeDescription *attribute = [self.entity.attributesByName valueForKey:key];
NSString *transformerName = [attribute valueTransformerName];
NSValueTransformer *valueTransformer = ( transformerName ? [NSValueTransformer valueTransformerForName:transformerName] : nil );
id transformedValue = [self valueForKey:key];
if ( valueTransformer ) transformedValue = [valueTransformer transformedValue:transformedValue];
return transformedValue;
}

- (id)reverseTransformedValueOfAttribute:(NSString *)key withValue:(id)value
{
NSAttributeDescription *attribute = [self.entity.attributesByName valueForKey:key];
NSString *transformerName = [attribute valueTransformerName];
NSValueTransformer *valueTransformer = ( transformerName ? [NSValueTransformer valueTransformerForName:transformerName] : nil );
if ( valueTransformer ) value = [valueTransformer reverseTransformedValue:value];
return value;
}

- (NSDictionary *)dictionaryOfAllAttributes
{
NSDictionary *objectAttributeNames = [[self entity] attributesByName];

NSMutableDictionary *attributeValues = [NSMutableDictionary dictionaryWithCapacity:[objectAttributeNames count]];
for( NSString *eachAttributeName in [objectAttributeNames allKeys] ) {
[attributeValues setValue:[self valueForKey:eachAttributeName] forKey:eachAttributeName];
NSAttributeDescription *attribute = [objectAttributeNames valueForKey:eachAttributeName];
if ( [attribute isTransient] ) continue;
id value = [self transformedValueOfAttribute:eachAttributeName];
[attributeValues setValue:value forKey:eachAttributeName];
}

return attributeValues;
Expand Down

0 comments on commit e57262e

Please sign in to comment.