Skip to content

Instantly share code, notes, and snippets.

@oks

oks/FEMMapping Secret

Created July 1, 2014 09:57
Show Gist options
  • Save oks/8c31a1c8b6c0fd0663d5 to your computer and use it in GitHub Desktop.
Save oks/8c31a1c8b6c0fd0663d5 to your computer and use it in GitHub Desktop.
JSON
id = 53b1709bb9e1a53e2ae05d8e;
phones = (
{
contact = 380931389130;
label = main;
status = verified;
}
);
//Mapping
+ (FEMManagedObjectMapping *)myProfileMapping
{
return [FEMManagedObjectMapping mappingForEntityName:[CDMyProfileEntity entityName] configuration:^(FEMManagedObjectMapping *mapping) {
mapping.primaryKey = kPropertyKeyProfileUserID;
[mapping addAttributeMappingDictionary:@{kPropertyKeyProfileUserID : @"id"}];
[mapping addRelationshipMapping:[self personalInfoMapping]
forProperty:kPropertyKeyProfilePersonalInfo
keyPath:@"profile"];
[mapping addToManyRelationshipMapping:[self socialMapping]
forProperty:kPropertyKeyProfileSocials
keyPath:@"socials"];
[mapping addToManyRelationshipMapping:[self communicationItemMapping]
forProperty:kPropertyKeyProfileCommunications
keyPath:@"phones"];
[mapping addToManyRelationshipMapping:[self communicationItemMapping]
forProperty:kPropertyKeyProfileCommunications
keyPath:@"emails"];
[mapping addAttributeMappingDictionary:@{kPropertyKeyProfileAccessLevel : @"acl"}];
}];
}
// Call
- (void)updateMyProfile:(NSDictionary*)profile
{
FEMManagedObjectMapping* mapping = [CDMappingProvider myProfileMapping];
if (mapping.primaryKey)
{
FEMAttributeMapping *primaryKeyMapping = mapping.primaryKeyMapping;
//get userID field from dict representation
id userIDValue = [primaryKeyMapping mappedValueFromRepresentation:profile];
if (userIDValue) // if not nil
{
//if saving userId NOT match curretly saved userID, and currect userID is exist
if (![[CDSettingsManager shared].userID isEqualToString:userIDValue] &&
[CDSettingsManager shared].userID.length)
{
//drop all data in database, including context and persistent store coordinator
[self removeProfileAndCleanAllData];
[CDDataAccessor setupContentDataBase];
}
// UPDATE existing object with data from dict representation
CDMyProfileEntity* myProfile = [CDDataBaseFetcher myProfile];
if (YES)
{
myProfile = [FEMManagedObjectDeserializer deserializeObjectExternalRepresentation:profile usingMapping:mapping context:[self serverSavingContext]];
}
else
{
[FEMManagedObjectDeserializer fillObject:myProfile fromExternalRepresentation:profile usingMapping:mapping];
}
[CDSettingsManager shared].userID = userIDValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment