简体   繁体   中英

how to properly update the array list data on realm database in swift?

I have product realm object like this:

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
        ]
    }

}

I want to update data of a product using this function:

 static func updateProductDataInRealmDatabase(oldProductData: Product, newProductData: Product) {

        guard let matchingProduct = RealmService.shared.realm.objects(Product.self).filter("productID == %@", oldProductData.productID).first else {return}

        var newDataProductDictionary = newProductData.toDictionary() // the definition of toDictionary is in above code
        newDataProductDictionary["productID"] = nil   // to avoid updating the primary key

        RealmService.shared.update(object: matchingProduct, for: newDataProductDictionary)

    }

and here is the definition of realm service to update data:

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)
        }

    }


}

I actually can update some of the data to be the new one like the productName , productPrice etc , But I can't update the imagePaths property which is a List<String>

the array will always to be zero after updating the data like the image below, the imagePaths array become zero :

在此处输入图片说明

so how to update the product data properly ? especially to update the List data in my realm object. what went wrong in here ? I use the code below to update the realm database :

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)
        }

    }

Since Product class already has the primary key, no need to search for the existing realm object using productID. Realm will add new object or update the old object with this query mentioned below.

You can query like this.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM