                 
             }
         }
-
+        
         return (insertedOrUpdatedManagedObjects,deletedManagedObjects)
     }
     
-    func insertOrUpdateManagedObjects(fromCKRecords ckRecords:Array<AnyObject>)->Bool
-    {
-        var predicate = NSPredicate(format: "%K == $ckRecordIDString",CKSIncrementalStoreLocalStoreRecordIDAttributeName)
-        
-        for object in ckRecords
-        {
-            var ckRecord:CKRecord = object as! CKRecord
-            var fetchRequest = NSFetchRequest(entityName: ckRecord.recordType)
-            var error:NSErrorPointer = nil
-            fetchRequest.predicate = predicate.predicateWithSubstitutionVariables(["ckRecordIDString":ckRecord.recordID.recordName])
-            fetchRequest.fetchLimit = 1
-            var results = self.localStoreMOC?.executeFetchRequest(fetchRequest, error: error)
-            
-            if error == nil && results?.count > 0
-            {
-                var managedObject = results?.first as! NSManagedObject
-                var recordIDString = managedObject.valueForKey(CKSIncrementalStoreLocalStoreRecordIDAttributeName) as! String
-                
-                var keys = ckRecord.allKeys().filter({(obj)->Bool in
-                    
-                    if ckRecord.objectForKey(obj as! String) is CKReference
-                    {
-                        return false
-                    }
-                    return true
-                })
-                var values = ckRecord.dictionaryWithValuesForKeys(keys)
-                managedObject.setValuesForKeysWithDictionary(values)
-                managedObject.setValue(NSNumber(short: CKSLocalStoreRecordChangeType.RecordNoChange.rawValue), forKey: CKSIncrementalStoreLocalStoreChangeTypeAttributeName)
-                
-                var data = NSMutableData()
-                var coder = NSKeyedArchiver(forWritingWithMutableData: data)
-                ckRecord.encodeSystemFieldsWithCoder(coder)
-                coder.finishEncoding()
-                managedObject.setValue(data, forKey: CKSIncrementalStoreLocalStoreRecordEncodedValuesAttributeName)
-                
-                var changedCKReferenceRecordIDStringsWithKeys = ckRecord.allKeys().filter({(obj)->Bool in
-                    
-                    if ckRecord.objectForKey(obj as! String) is CKReference
-                    {
-                        return true
-                    }
-                    return false
-                }).map({(obj)->(key:String,recordIDString:String) in
-                    
-                    var key:String = obj as! String
-                    return (key,(ckRecord.objectForKey(key) as! CKReference).recordID.recordName)
-                })
-                
-                for object in changedCKReferenceRecordIDStringsWithKeys
-                {
-                    var attributeEntityName = (managedObject.entity.relationshipsByName[object.key] as! NSRelationshipDescription).destinationEntity?.name
-                    var fetchRequest = NSFetchRequest(entityName: attributeEntityName!)
-                    fetchRequest.predicate = NSPredicate(format: "%K == %@", CKSIncrementalStoreLocalStoreRecordIDAttributeName,object.recordIDString)
-                    var error:NSErrorPointer = nil
-                    var results = self.localStoreMOC?.executeFetchRequest(fetchRequest, error: error)
-                    if error == nil && results!.count > 0
-                    {
-                        managedObject.setValue(results?.first, forKey: object.key)
-                        break
-                    }
-                    
-                }
-            }
-            else
-            {
-                var managedObject:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName(ckRecord.recordType, inManagedObjectContext: self.localStoreMOC!) as! NSManagedObject
-                var keys = ckRecord.allKeys().filter({(object)->Bool in
-                    
-                    var key:String = object as! String
-                    if ckRecord.objectForKey(key) is CKReference
-                    {
-                        return false
-                    }
-                    
-                    return true
-                })
-                var data = NSMutableData()
-                var coder = NSKeyedArchiver(forWritingWithMutableData: data)
-                ckRecord.encodeSystemFieldsWithCoder(coder)
-                coder.finishEncoding()
-                managedObject.setValue(data, forKey: CKSIncrementalStoreLocalStoreRecordEncodedValuesAttributeName)
-                var changedCKReferencesRecordIDsWithKeys = ckRecord.allKeys().filter({(object)->Bool in
-                    
-                    var key:String = object as! String
-                    if ckRecord.objectForKey(key) is CKReference
-                    {
-                        return true
-                    }
-                    return false
-                    
-                }).map({(object)->(key:String,recordIDString:String) in
-                    
-                    var key:String = object as! String
-                    
-                    return (key,(ckRecord.objectForKey(key) as! CKReference).recordID.recordName)
-                })
-                
-                var values = ckRecord.dictionaryWithValuesForKeys(keys)
-                managedObject.setValuesForKeysWithDictionary(values)
-                managedObject.setValue(NSNumber(short: CKSLocalStoreRecordChangeType.RecordNoChange.rawValue), forKey: CKSIncrementalStoreLocalStoreChangeTypeAttributeName)
-                managedObject.setValue(ckRecord.recordID.recordName, forKey: CKSIncrementalStoreLocalStoreRecordIDAttributeName)
-                
-                
-                for object in changedCKReferencesRecordIDsWithKeys
-                {
-                    var ckReferenceRecordIDString:String = object.recordIDString
-                    var referenceManagedObject = Array(self.localStoreMOC!.registeredObjects).filter({(object)->Bool in
-                        
-                        var managedObject:NSManagedObject = object as! NSManagedObject
-                        if (managedObject.valueForKey(CKSIncrementalStoreLocalStoreRecordIDAttributeName) as! String) == ckReferenceRecordIDString
-                        {
-                            return true
-                        }
-                        return false
-                    }).first
-                    
-                    if referenceManagedObject != nil
-                    {
-                        managedObject.setValue(referenceManagedObject, forKey: object.key)
-                    }
-                    else
-                    {
-                        var attributeEntityName = (managedObject.entity.relationshipsByName[object.key] as! NSRelationshipDescription).destinationEntity?.name
-                        var fetchRequest = NSFetchRequest(entityName: attributeEntityName!)
-                        fetchRequest.predicate = NSPredicate(format: "%K == %@", CKSIncrementalStoreLocalStoreRecordIDAttributeName,ckReferenceRecordIDString)
-                        fetchRequest.fetchLimit = 1
-                        var error:NSErrorPointer = nil
-                        var results = self.localStoreMOC?.executeFetchRequest(fetchRequest, error: error)
-                        if error == nil && results?.count > 0
-                        {
-                            managedObject.setValue(results?.first as! NSManagedObject, forKey: object.key)
-                            break
-                        }
-                    }
-                }
-            }
-        }
-        
-        if self.localStoreMOC!.hasChanges
-        {
-            var error:NSErrorPointer = nil
-            self.localStoreMOC!.save(error)
-            if error == nil
-            {
-                return true
-            }
-        }
-        else
-        {
-            return true
-        }
-        return false
-    }
-    
-    func deleteManagedObjects(fromCKRecordIDs ckRecordIDs:Array<AnyObject>)->Bool
-    {
-        var predicate = NSPredicate(format: "%K IN $ckRecordIDs",CKSIncrementalStoreLocalStoreRecordIDAttributeName)
-        
-        var ckRecordIDStrings = ckRecordIDs.map({(object)->String in
-            
-            var ckRecordID:CKRecordID = object as! CKRecordID
-            return ckRecordID.recordName
-        })
-        var entityNames = self.localStoreMOC?.persistentStoreCoordinator?.managedObjectModel.entitiesByName.keys.array
-        
-        for name in entityNames!
-        {
-            var fetchRequest = NSFetchRequest(entityName: name as! String)
-            fetchRequest.predicate = predicate.predicateWithSubstitutionVariables(["ckRecordIDs":ckRecordIDStrings])
-            var error:NSErrorPointer = nil
-            var results = self.localStoreMOC?.executeFetchRequest(fetchRequest, error: error)
-            if error == nil && results?.count > 0
-            {
-                for object in results as! [NSManagedObject]
-                {
-                    self.localStoreMOC?.deleteObject(object)
-                }
-                
-            }
-        }
-        
-        var error:NSErrorPointer = nil
-        self.localStoreMOC?.save(error)
-        
-        
-        if error == nil
-        {
-            return true
-        }
-        
-        return false
+    func localChangesInServerRepresentation(#localChanges:(insertedOrUpdatedManagedObjects:Array<AnyObject>,deletedManagedObjects:Array<AnyObject>))->(insertedOrUpdatedCKRecords:Array<CKRecord>,deletedCKRecordIDs:Array<CKRecordID>)
+    {
+        return (self.insertedOrUpdatedCKRecords(fromManagedObjects: localChanges.insertedOrUpdatedManagedObjects),self.deletedCKRecordIDs(fromManagedObjects: localChanges.deletedManagedObjects))
     }
     
     func insertedOrUpdatedCKRecords(fromManagedObjects managedObjects:Array<AnyObject>)->Array<CKRecord>
     {
-         return managedObjects.map({(object)->CKRecord in
+        return managedObjects.map({(object)->CKRecord in
             
             var managedObject:NSManagedObject = object as! NSManagedObject
             var ckRecordID = CKRecordID(recordName: (managedObject.valueForKey(CKSIncrementalStoreLocalStoreRecordIDAttributeName) as! String), zoneID: CKRecordZoneID(zoneName: CKSIncrementalStoreCloudDatabaseCustomZoneName, ownerName: CKOwnerDefaultName))

            
            return ckRecordID
        })
    }
}


        return self.applyServerChangesToLocalDatabase(insertedOrUpdatedCKRecordsFromServer, deletedCKRecordIDs: deletedCKRecordIDsFromServer)
        
    }

    // MARK: Server Change Token
    func savedCKServerChangeToken()->CKServerChangeToken?
    {
        if NSUserDefaults.standardUserDefaults().objectForKey(CKSIncrementalStoreSyncOperationFetchChangeTokenKey) != nil
