diff --git a/Source/Sync/NSManagedObject+Sync.swift b/Source/Sync/NSManagedObject+Sync.swift index 1d432cdd..dcad561a 100644 --- a/Source/Sync/NSManagedObject+Sync.swift +++ b/Source/Sync/NSManagedObject+Sync.swift @@ -199,8 +199,6 @@ extension NSManagedObject { /// - objectJSONBlock: A block that gives the oportunity to the called to act on the current processed JSON. /// - Throws: An exception that will throw if any of the underlaying operations fail. 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 { @@ -306,7 +304,7 @@ extension NSManagedObject { var childPredicate: NSPredicate? let manyToMany = inverseIsToMany && relationship.isToMany - var childOperations = updatedOperations + var childOperations = operations let childrenIDs = (((childIDs as Any) as AnyObject) as? NSArray) ?? NSArray() if manyToMany { diff --git a/Source/Sync/Sync.swift b/Source/Sync/Sync.swift index c7fde3d4..67216472 100644 --- a/Source/Sync/Sync.swift +++ b/Source/Sync/Sync.swift @@ -27,28 +27,7 @@ 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 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 - } + public static let all: OperationOptions = [.insert, .update, .delete] } var downloadFinished = false diff --git a/Tests/Sync/SyncTests.swift b/Tests/Sync/SyncTests.swift index 0be70351..7d01979c 100644 --- a/Tests/Sync/SyncTests.swift +++ b/Tests/Sync/SyncTests.swift @@ -1583,132 +1583,6 @@ class SyncTests: XCTestCase { dataStack.drop() } - - // https://github.com/3lvis/Sync/issues/422 - - // ----------- - // One to many - // ----------- - - func test422OneToManyOperationOptionsInsert() { - let dataStack = Helper.dataStackWithModelName("422OneToMany") - let initial = Helper.objectsFromJSON("422-one-to-many-insert-option-initial.json") as! [[String: Any]] - - dataStack.sync(initial, inEntityNamed: "User", completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updated = Helper.objectsFromJSON("422-one-to-many-insert-option-update.json") as! [[String: Any]] - dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .insertRelationships], completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2) - - let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first - XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "101") - - dataStack.drop() - } - - func test422OneToManyOperationOptionsUpdateRelationships() { - let dataStack = Helper.dataStackWithModelName("422OneToMany") - - let initial = Helper.objectsFromJSON("422-one-to-many-update-option-initial.json") as! [[String: Any]] - dataStack.sync(initial, inEntityNamed: "User", completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updated = Helper.objectsFromJSON("422-one-to-many-update-option-update.json") as! [[String: Any]] - dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .updateRelationships], completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first - XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "updated-101") - - dataStack.drop() - } - - func test422OneToManyOperationOptionsDeleteRelationships() { - let dataStack = Helper.dataStackWithModelName("422OneToMany") - - let initial = Helper.objectsFromJSON("422-one-to-many-delete-option-initial.json") as! [[String: Any]] - dataStack.sync(initial, inEntityNamed: "User", completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2) - - let updated = Helper.objectsFromJSON("422-one-to-many-delete-option-update.json") as! [[String: Any]] - dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .deleteRelationships], completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 102"), inContext: dataStack.mainContext).first - XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "102") - - dataStack.drop() - } - - // ----------- - // Many to many - // ----------- - - func test422ManyToManyOperationOptionsInsert() { - let dataStack = Helper.dataStackWithModelName("422ManyToMany") - let initial = Helper.objectsFromJSON("422-many-to-many-insert-option-initial.json") as! [[String: Any]] - - dataStack.sync(initial, inEntityNamed: "User", completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updated = Helper.objectsFromJSON("422-many-to-many-insert-option-update.json") as! [[String: Any]] - dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .insertRelationships], completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2) - - let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first - XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "101") - - dataStack.drop() - } - - func test422ManyToManyOperationOptionsUpdateRelationships() { - let dataStack = Helper.dataStackWithModelName("422ManyToMany") - - let initial = Helper.objectsFromJSON("422-many-to-many-update-option-initial.json") as! [[String: Any]] - dataStack.sync(initial, inEntityNamed: "User", completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updated = Helper.objectsFromJSON("422-many-to-many-update-option-update.json") as! [[String: Any]] - dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .updateRelationships], completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1) - - let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first - XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "updated-101") - - dataStack.drop() - } - - func test422ManyToManyOperationOptionsDeleteRelationships() { - let dataStack = Helper.dataStackWithModelName("422ManyToMany") - - let initial = Helper.objectsFromJSON("422-many-to-many-delete-option-initial.json") as! [[String: Any]] - dataStack.sync(initial, inEntityNamed: "User", completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2) - - let updated = Helper.objectsFromJSON("422-many-to-many-delete-option-update.json") as! [[String: Any]] - dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .deleteRelationships], completion: nil) - XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1) - // count the messages that user 1 has instead of counting all messages - let userMessageCount = Helper.countForEntity("Message", predicate: NSPredicate(format: "ANY users.id = 1"), inContext: dataStack.mainContext) - XCTAssertEqual(userMessageCount, 1) - - let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 102"), inContext: dataStack.mainContext).first - XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "102") - - dataStack.drop() - } // https://github.com/3lvis/Sync/issues/476 func test476() {