Skip to content

Commit

Permalink
Merge pull request 3lvis#148 from hyperoslo/feature/relationship-by-ud
Browse files Browse the repository at this point in the history
Feature: Sync relationships using id
  • Loading branch information
3lvis committed Nov 28, 2015
2 parents 62654de + d02d755 commit e99e49f
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 59 deletions.
4 changes: 4 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
141894A71BDD64AF00EE52CE /* Networking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141894A31BDD64AF00EE52CE /* Networking.swift */; };
141894A81BDD64AF00EE52CE /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141894A41BDD64AF00EE52CE /* User.swift */; };
141894A91BDD64AF00EE52CE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141894A51BDD64AF00EE52CE /* ViewController.swift */; };
14959D141C065350004EF790 /* notes_with_user_id.json in Resources */ = {isa = PBXBuildFile; fileRef = 14959D131C065350004EF790 /* notes_with_user_id.json */; };
8C1190CE4B3821813B6A3421 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E482492041434DB374B1948 /* Pods.framework */; };
BD2A2CB4A6B4EC694D5E358A /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E482492041434DB374B1948 /* Pods.framework */; };
E9FABD68F0CA454B8D54104E /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E482492041434DB374B1948 /* Pods.framework */; };
Expand Down Expand Up @@ -166,6 +167,7 @@
141894A51BDD64AF00EE52CE /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
146D72AC1AB782920058798C /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
146D72B11AB782920058798C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
14959D131C065350004EF790 /* notes_with_user_id.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = notes_with_user_id.json; sourceTree = "<group>"; };
14C0AF7F1BD6D4230009ECBE /* .slather.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .slather.yml; sourceTree = "<group>"; };
14C0AF801BD6D4230009ECBE /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .travis.yml; sourceTree = "<group>"; };
14C0AF811BD6D4230009ECBE /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -256,6 +258,7 @@
141894211BDD62F600EE52CE /* users_c.json */,
141894221BDD62F600EE52CE /* users_company.json */,
141894231BDD62F600EE52CE /* users_notes.json */,
14959D131C065350004EF790 /* notes_with_user_id.json */,
);
path = JSONs;
sourceTree = "<group>";
Expand Down Expand Up @@ -548,6 +551,7 @@
buildActionMask = 2147483647;
files = (
1418944C1BDD62F600EE52CE /* numbers.json in Resources */,
14959D141C065350004EF790 /* notes_with_user_id.json in Resources */,
141894421BDD62F600EE52CE /* bug-113-stories-comments-no-ids.json in Resources */,
141894571BDD62F600EE52CE /* users_company.json in Resources */,
141894441BDD62F600EE52CE /* bug-125.json in Resources */,
Expand Down
29 changes: 16 additions & 13 deletions Source/NSManagedObject+Sync.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ + (NSManagedObject *)sync_safeObjectInContext:(NSManagedObjectContext *)context
parent:(NSManagedObject *)parent
parentRelationshipName:(NSString *)relationshipName
error:(NSError **)error {
if(!remoteID) {
if (!remoteID) {
return [parent valueForKey:relationshipName];
}

Expand Down Expand Up @@ -54,7 +54,10 @@ - (void)sync_processRelationshipsUsingDictionary:(NSDictionary *)objectDictionar
NSArray *relationships = [self.entity sync_relationships];

for (NSRelationshipDescription *relationship in relationships) {
NSString *keyName = [[relationship.destinationEntity.name lowercaseString] stringByAppendingString:@"_id"];
NSEntityDescription *entity = [NSEntityDescription entityForName:relationship.entity.name inManagedObjectContext:self.managedObjectContext];
NSArray<NSRelationshipDescription *> *relationships = [entity relationshipsWithDestinationEntity:relationship.destinationEntity];
NSString *keyName = [[relationships.firstObject.name hyp_remoteString] stringByAppendingString:@"_id"];
NSLog(@"keyName: %@", keyName);
if (relationship.isToMany) {
[self sync_processToManyRelationship:relationship
usingDictionary:objectDictionary
Expand All @@ -66,13 +69,14 @@ - (void)sync_processRelationshipsUsingDictionary:(NSDictionary *)objectDictionar
forKey:relationship.name];
} else if ([objectDictionary objectForKey:keyName]) {
NSError *error = nil;
[self sync_processIdRelationship:relationship
[self sync_processIDRelationship:relationship
remoteID:[objectDictionary objectForKey:keyName]
andParent:parent
dataStack:dataStack
error:&error];
andParent:parent
dataStack:dataStack
error:&error];
} else {
NSError *error = nil;

[self sync_processToOneRelationship:relationship
usingDictionary:objectDictionary
andParent:parent
Expand Down Expand Up @@ -164,14 +168,13 @@ - (void)sync_processToOneRelationship:(NSRelationshipDescription *)relationship
}
}

- (void)sync_processIdRelationship:(NSRelationshipDescription *)relationship
remoteID:(NSNumber *)remoteID
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack
error:(NSError **)error {

- (void)sync_processIDRelationship:(NSRelationshipDescription *)relationship
remoteID:(NSNumber *)remoteID
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack
error:(NSError **)error {
NSString *entityName = relationship.destinationEntity.name;

NSError *errors = nil;
NSManagedObject *object = [NSManagedObject sync_safeObjectInContext:self.managedObjectContext
entityName:entityName
Expand Down
13 changes: 8 additions & 5 deletions Source/Sync.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ + (void)changes:(NSArray *)changes
dataStack:(DATAStack *)dataStack
completion:(void (^)(NSError *error))completion {
[dataStack performInNewBackgroundContext:^(NSManagedObjectContext *backgroundContext) {

[self changes:changes
inEntityNamed:entityName
predicate:predicate
Expand All @@ -47,11 +46,13 @@ + (void)changes:(NSArray *)changes
dataStack:(DATAStack *)dataStack
completion:(void (^)(NSError *error))completion {
[dataStack performInNewBackgroundContext:^(NSManagedObjectContext *backgroundContext) {

NSError *error = nil;
NSManagedObject *safeParent = [parent sync_copyInContext:backgroundContext
error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", [parent.entity.name lowercaseString], safeParent];

NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:backgroundContext];
NSArray<NSRelationshipDescription *> *relationships = [entity relationshipsWithDestinationEntity:parent.entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", relationships.firstObject.name, safeParent];

[self changes:changes
inEntityNamed:entityName
Expand Down Expand Up @@ -87,15 +88,17 @@ + (void)changes:(NSArray *)changes
}
}

/*if (predicate) {
/*
if (predicate) {
NSArray *processedChanges = [changes preprocessForEntityNamed:entityName
usingPredicate:predicate
parent:parent
dataStack:dataStack];
if (processedChanges.count > 0) {
changes = processedChanges;
}
}*/
}
*/

[DATAFilter changes:changes
inEntityNamed:entityName
Expand Down
27 changes: 27 additions & 0 deletions Tests/JSONs/notes_with_user_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"id": 0,
"text": "Melisa White's diary, episode 0",
"super_user_id": 0
},
{
"id": 1,
"text": "Melisa White's diary, episode 1",
"super_user_id": 0
},
{
"id": 2,
"text": "Melisa White's diary, episode 2",
"super_user_id": 0
},
{
"id": 3,
"text": "Glass Oconnor's diary, episode 0",
"super_user_id": 1
},
{
"id": 4,
"text": "Glass Oconnor's diary, episode 1",
"super_user_id": 1
}
]
8 changes: 4 additions & 4 deletions Tests/JSONs/tagged_notes.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"id": 0,
"text": "Shawn Merril's diary, episode 0",
"tags": [
"super_tags": [
{
"id": 1,
"name": "diary"
Expand All @@ -16,7 +16,7 @@
{
"id": 1,
"text": "Shawn Merril's diary, episode 1",
"tags": [
"super_tags": [
{
"id": 1,
"name": "diary"
Expand All @@ -26,7 +26,7 @@
{
"id": 2,
"text": "Shawn Merril's diary, episode 2",
"tags": [
"super_tags": [
{
"id": 1,
"name": "diary"
Expand All @@ -36,7 +36,7 @@
{
"id": 3,
"text": "Shawn Merril's diary, episode 3",
"tags": [
"super_tags": [
{
"id": 1,
"name": "diary"
Expand Down
22 changes: 11 additions & 11 deletions Tests/Models/Notes.xcdatamodeld/Demo.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="7549" systemVersion="14E7f" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="Note" syncable="YES">
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="9057" systemVersion="15B42" minimumToolsVersion="Automatic">
<entity name="SuperNote" syncable="YES">
<attribute name="remoteID" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
<attribute name="text" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="tags" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Tag" inverseName="notes" inverseEntity="Tag" syncable="YES"/>
<relationship name="user" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="User" inverseName="notes" inverseEntity="User" syncable="YES"/>
<relationship name="superTags" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="SuperTag" inverseName="superNotes" inverseEntity="SuperTag" syncable="YES"/>
<relationship name="superUser" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="SuperUser" inverseName="superNotes" inverseEntity="SuperUser" syncable="YES"/>
</entity>
<entity name="Tag" syncable="YES">
<entity name="SuperTag" syncable="YES">
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="remoteID" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
<relationship name="notes" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Note" inverseName="tags" inverseEntity="Note" syncable="YES"/>
<relationship name="superNotes" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="SuperNote" inverseName="superTags" inverseEntity="SuperNote" syncable="YES"/>
</entity>
<entity name="User" representedClassName="" syncable="YES">
<entity name="SuperUser" representedClassName="" syncable="YES">
<attribute name="createdAt" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="email" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="remoteID" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
<attribute name="updatedAt" optional="YES" attributeType="Date" syncable="YES"/>
<relationship name="notes" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Note" inverseName="user" inverseEntity="Note" syncable="YES">
<relationship name="superNotes" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="SuperNote" inverseName="superUser" inverseEntity="SuperNote" syncable="YES">
<userInfo>
<entry key="hyper.remoteKey" value="annotations"/>
</userInfo>
</relationship>
</entity>
<elements>
<element name="Note" positionX="-20" positionY="18" width="128" height="103"/>
<element name="Tag" positionX="160" positionY="0" width="128" height="90"/>
<element name="User" positionX="-207" positionY="-15" width="128" height="135"/>
<element name="SuperNote" positionX="-20" positionY="18" width="128" height="105"/>
<element name="SuperTag" positionX="160" positionY="0" width="128" height="90"/>
<element name="SuperUser" positionX="-207" positionY="-15" width="128" height="135"/>
</elements>
</model>
Loading

0 comments on commit e99e49f

Please sign in to comment.