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

Feature: Extend operation options #433

Merged
merged 14 commits into from
Jan 30, 2018
198 changes: 198 additions & 0 deletions Project.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Source/Sync/NSManagedObject+Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ extension NSManagedObject {
- parameter dataStack: The DataStack instance.
*/
func sync_toManyRelationship(_ relationship: NSRelationshipDescription, dictionary: [String: Any], parent: NSManagedObject?, parentRelationship: NSRelationshipDescription?, context: NSManagedObjectContext, operations: Sync.OperationOptions, shouldContinueBlock: (() -> Bool)?, objectJSONBlock: ((_ objectJSON: [String: Any]) -> [String: Any])?) throws {
let updatedOperations = operations.relationshipOperations()

var children: [[String: Any]]?
let childrenIsNull = (relationship.customKey as Any?) is NSNull || dictionary[relationship.name.hyp_snakeCase()] is NSNull || dictionary[relationship.name] is NSNull
if childrenIsNull {
Expand Down Expand Up @@ -300,7 +302,7 @@ extension NSManagedObject {

var childPredicate: NSPredicate?
let manyToMany = inverseIsToMany && relationship.isToMany
var childOperations = operations
var childOperations = updatedOperations
if manyToMany {
childOperations.remove(.delete)
if ((childIDs as Any) as AnyObject).count > 0 {
Expand Down
23 changes: 22 additions & 1 deletion Source/Sync/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,28 @@ public protocol SyncDelegate: class {
public static let insert = OperationOptions(rawValue: 1 << 0)
public static let update = OperationOptions(rawValue: 1 << 1)
public static let delete = OperationOptions(rawValue: 1 << 2)
public static let all: OperationOptions = [.insert, .update, .delete]
public static let insertRelationships = OperationOptions(rawValue: 1 << 3)
public static let updateRelationships = OperationOptions(rawValue: 1 << 4)
public static let deleteRelationships = OperationOptions(rawValue: 1 << 5)
public static let all: OperationOptions = [.insert, .update, .delete, .insertRelationships, .updateRelationships, .deleteRelationships]

func relationshipOperations() -> OperationOptions {
var options = OperationOptions.all

if !self.contains(.insertRelationships) {
options.remove(.insert)
}

if !self.contains(.updateRelationships) {
options.remove(.update)
}

if !self.contains(.deleteRelationships) {
options.remove(.delete)
}

return options
}
}

var downloadFinished = false
Expand Down
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-many-to-many-delete-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
},
{
"id": 102,
"text": "102"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-many-to-many-delete-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102-updated"
},
{
"id": 103,
"text": "103"
}
]
}
]
11 changes: 11 additions & 0 deletions Tests/Sync/JSONs/422-many-to-many-insert-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-many-to-many-insert-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102"
},
{
"id": 101,
"text": "updated-101"
}
]
}
]
11 changes: 11 additions & 0 deletions Tests/Sync/JSONs/422-many-to-many-update-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-many-to-many-update-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102"
},
{
"id": 101,
"text": "updated-101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-many-delete-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
},
{
"id": 102,
"text": "102"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-many-delete-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102-updated"
},
{
"id": 103,
"text": "103"
}
]
}
]
11 changes: 11 additions & 0 deletions Tests/Sync/JSONs/422-one-to-many-insert-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-many-insert-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102"
},
{
"id": 101,
"text": "updated-101"
}
]
}
]
11 changes: 11 additions & 0 deletions Tests/Sync/JSONs/422-one-to-many-update-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-many-update-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102"
},
{
"id": 101,
"text": "updated-101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-one-delete-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
},
{
"id": 102,
"text": "102"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-one-delete-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102-updated"
},
{
"id": 103,
"text": "103"
}
]
}
]
11 changes: 11 additions & 0 deletions Tests/Sync/JSONs/422-one-to-one-insert-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-one-insert-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102"
},
{
"id": 101,
"text": "updated-101"
}
]
}
]
11 changes: 11 additions & 0 deletions Tests/Sync/JSONs/422-one-to-one-update-option-initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"id": 1,
"messages": [
{
"id": 101,
"text": "101"
}
]
}
]
15 changes: 15 additions & 0 deletions Tests/Sync/JSONs/422-one-to-one-update-option-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 1,
"messages": [
{
"id": 102,
"text": "102"
},
{
"id": 101,
"text": "updated-101"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="17D47" minimumToolsVersion="Xcode 7.0" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="Message" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<attribute name="text" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="users" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="User" inverseName="messages" inverseEntity="User" syncable="YES"/>
</entity>
<entity name="User" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="messages" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="Message" inverseName="users" inverseEntity="Message" syncable="YES"/>
</entity>
<elements>
<element name="Message" positionX="-54" positionY="-9" width="128" height="90"/>
<element name="User" positionX="-297" positionY="-12" width="128" height="75"/>
</elements>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13241" systemVersion="16G29" minimumToolsVersion="Xcode 7.0" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="Message" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<attribute name="text" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="user" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="User" inverseName="messages" inverseEntity="User" syncable="YES"/>
</entity>
<entity name="User" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="messages" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="Message" inverseName="user" inverseEntity="Message" syncable="YES"/>
</entity>
<elements>
<element name="User" positionX="-297" positionY="-12" width="128" height="75"/>
<element name="Message" positionX="-54" positionY="-9" width="128" height="88"/>
</elements>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="17C88" minimumToolsVersion="Xcode 7.0" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="Author" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="book" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="Book" inverseName="author" inverseEntity="Book" syncable="YES"/>
</entity>
<entity name="Book" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="author" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Author" inverseName="book" inverseEntity="Author" syncable="YES"/>
</entity>
<elements>
<element name="Book" positionX="-54" positionY="-9" width="128" height="73"/>
<element name="Author" positionX="-297" positionY="-12" width="128" height="73"/>
</elements>
</model>
Loading