Agung
Agung

Reputation: 13873

how to update realm object based on the certain property in Realm?

I have products in my Realm database like this enter image description here

I want to update my realm database based on productID, so I don't need to add another product over and over again. let say I want to update quantity of product that has productID = "a" to be 5.

I have tried to write something like this.

        let selectedProductID = "a"
        let productsInRealmDatabase = realm.objects(Product.self)
        let productIndex = productsInRealmDatabase.index(where: {$0.productID == selectedProductID})
        
        if let productIndex = productIndex {
        
        do {
            
            try realm.write {
                var productRealm = productsInRealmDatabase[productIndex]
                productRealm.quantity = 5
                productsInRealmDatabase[productIndex] = productRealm // ERROR HERE
            }
            
        } catch {
            // error Handling
        }
        
        
    }

but I got error in : productsInRealmDatabase[productIndex] = productRealm

Error Message: Cannot assign through subscript: subscript is get-only

so how to update realm object based on the certain property in Realm?

Upvotes: 0

Views: 1794

Answers (3)

Pooja Singh
Pooja Singh

Reputation: 136

While inserting data to your database inside the insert function mark update key as true and then try updating the value. eg:

   static func insertData() {

        //Your insertion code//
         try! realm.write {
                realm.add(request, update:  true)
            }
   }


   static func updateData(productId: String, quantity: Int) {

    let product = self.getProductData(prodId: productId)
    let realm = try! Realm()
    try! realm.write {
        product?.quantity = quantity
    }
}

Hope this helps you out.

Upvotes: 0

David Pasztor
David Pasztor

Reputation: 54785

You should use Realm's own filter method which accepts an NSPredicate and returns an auto-updating Results instance rather than Swift's filter when operating on Realm collections. Than either update the properties of the fetched prouduct or create a new one and save that to Realm.

let selectedProductID = "a"
let productsInRealmDatabase = realm.objects(Product.self)
let matchingProduct = productsInRealmDatabase.filter("productID == %@", selectedProductID).first

if let matchingProduct = matchingProduct {
    do {
        try realm.write {
            matchingProduct.quantity = 5
        }
    } catch {
        // error Handling
    }
} else {
    let newProduct = Product()
    newProduct.productID = selectedProductID
    newProduct.quantity = 5
    do {
        try realm.write {
            realm.add(newProduct)
        }
    } catch {
        // error Handling
    }
}

If you want your Products to be unique based on their productID property, you use also set productID as the primaryKey of your Object subclass.

class Product:Object {
    @objc dynamic var productID = ""
    ...
    override static func primaryKey() -> String? {
        return "productID"
    }
}

Upvotes: 3

Aakash
Aakash

Reputation: 2269

Try this -

let selectedProductID = "a"
let productsInRealmDatabase = realm.objects(Product.self)
let filteredProducts = productsInRealmDatabase.filter("productID = \(selectedProductID)")

do {
    try realm.write {
        filteredProducts.forEach { product in
            product.quantity = 5
        }
    }
} catch {
    // error Handling
}

Upvotes: 0

Related Questions