     
     func updateCollectionCenters(centers: @escaping () -> Void, failure: @escaping (_ error: Error) -> Void) {
         networkManager.get(url: kCollectionCentersUrl, completion: { result in
-            guard let array = result as? [Any]  else {
+            guard let array = result as? [[String: Any]]  else {
                 DispatchQueue.main.async { centers() }
                 return
             }
-            
-            let realm = try! Realm()
-            let storedCenters = realm.objects(JUWCollectionCenter.self)
-            for localCenter in storedCenters {
-                let predicate = NSPredicate(format: "id == %@", localCenter.centerIdentifier)
-                let foundItems = array.filter{ predicate.evaluate(with: $0) }
-                if foundItems.count == 0 {
-                    #if DEBUG
-                        print("center not anymore in server", localCenter.centerIdentifier)
-                    #endif
-                    try! realm.write {
-                        realm.delete(localCenter)
-                    }
-                }
-            }
-            
-            for centerValue in array {
-                if let dictionary = centerValue as? [String: Any] {
-                    let predicate = NSPredicate(format: "centerIdentifier = %@", dictionary["id"] as! String)
-                    var center = realm.objects(JUWCollectionCenter.self).filter(predicate).first
-                    if center == nil {
-                        center = JUWCollectionCenter()
-                        try! realm.write {
-                            realm.add(center!)
-                        }
-                    }
-                    
-                    try! realm.write {
-                        if let name = dictionary["nombre"] as? String {
-                            center?.name = name
-                        }
-                        
-                        if let address = dictionary["direccion"] as? String {
-                            center?.address = address
-                        }
-                        
-                        if let identifier = dictionary["id"] as? String {
-                            center?.centerIdentifier = identifier
-                        }
-                        
-                        if let geoPosDictionary = dictionary["geopos"] as? [String: Any] {
-                            if geoPosDictionary.count > 0 {
-                                if let latitude = geoPosDictionary["lat"] as? Double {
-                                    center?.latitude = latitude
-                                }
-                                if let longitude = geoPosDictionary["lng"] as? Double {
-                                    center?.longitude = longitude
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-            DispatchQueue.main.async {
-                centers()
-            }
+            let realm = try? Realm()
+            realm?.deleteCollectionCenters(notIn: array)
+            realm?.addCollectionCenters(in: array)
+            DispatchQueue.main.async { centers() }
         }) { (error) in
             failure(error!)
         }

        
    }
}

    func collectionCenters(whichNeed product: String, completion: @escaping (_ result: [JUWCollectionCenter]) -> Void) {
        let query = product.lowercased().stripCharacters(in: CharacterSet.alphanumerics.inverted)
        precondition(!query.isEmpty, "Query should not be empty")
        guard let url = String(format: kCollectionCenterSearchProductUrl, query).encoded() else {
            DispatchQueue.main.async { completion([]) }
            return
        }
        networkManager.get(url: url, completion: { (result) in
            guard let array = result as? [[String: Any]]  else {
                DispatchQueue.main.async { completion([]) }
                return
            }
            let realm = try! Realm()
            let centers = realm.collectionCenters(with: array)
            DispatchQueue.main.async { completion(centers) }
        }) { (error) in
            DispatchQueue.main.async { completion([]) }
        }
    }
    
