Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中正确更新领域数据库上的数组列表数据?_Ios_Swift_Realm - Fatal编程技术网

Ios 如何在swift中正确更新领域数据库上的数组列表数据?

Ios 如何在swift中正确更新领域数据库上的数组列表数据?,ios,swift,realm,Ios,Swift,Realm,我有这样的产品领域对象: class Product : Object { @objc dynamic var productID : String = "" @objc dynamic var name : String = "" @objc dynamic var unitPrice: Double = 0.0 @objc dynamic var quantity = 0 @objc dynamic var descriptionProduct :

我有这样的产品领域对象:

class Product : Object {

    @objc dynamic var productID : String = ""
    @objc dynamic var name : String = ""
    @objc dynamic var unitPrice: Double = 0.0
    @objc dynamic var quantity = 0
    @objc dynamic var descriptionProduct : String = ""
    @objc dynamic var hasBeenAddedToWishList : Bool = false
    @objc dynamic var hasBeenAddedToCart : Bool = false
    @objc dynamic var isNewProduct : Bool = false
    var imagePaths = List<String>()

    override static func primaryKey() -> String? {
        return "productID"
    }

    func toDictionary() -> [String:Any] {
        return [
            "productID" : productID,
            "name" : name,
            "unitPrice": unitPrice,
            "quantity": quantity,
            "descriptionProduct": descriptionProduct,
            "hasBeenAddedToWishList": hasBeenAddedToWishList,
            "hasBeenAddedToCart": hasBeenAddedToCart,
            "isNewProduct": isNewProduct,
            "imagePaths": imagePaths
        ]
    }

}
下面是更新数据的领域服务的定义:

class RealmService {

    private init() {}
    static let shared = RealmService()

    var realm = try! Realm()

    func update<T: Object>(object: T, for dictionary: [String: Any]) {

        do {

            try realm.write {

                for (key,value) in dictionary {
                    object.setValue(value, forKey: key)
                }

            }

        } catch {
            post(error)
        }

    }


}

由于Product类已经具有主键,因此无需使用productID搜索现有领域对象。 Realm将使用下面提到的查询添加新对象或更新旧对象

您可以这样查询

let realm = try! Realm()

try! realm.write {
realm.add(anyProductObject,update: true)
}
// You can customize this using generics and also use try catch to handle the errors in case you pass the non realm defined class object 
func update<T: Object>(object: T, for dictionary: [String: Any]) {

        do {

            try realm.write {

                for (key,value) in dictionary {
                    object.setValue(value, forKey: key)
                }

            }

        } catch {
            post(error)
        }

    }
let realm = try! Realm()

try! realm.write {
realm.add(anyProductObject,update: true)
}
// You can customize this using generics and also use try catch to handle the errors in case you pass the non realm defined class object