Search code examples
iosswiftrealm

Append existing object to Realm List


I have CatalogItem objects with primary key in my code. Realm is throwing an error during write in the following code:

class CartItem: Object {
    dynamic var id: Int = 0
    dynamic var item: CatalogItem!
    dynamic var amount: Int = 0

    convenience required init(id: Int, item: CatalogItem, amount: Int) {
        self.init()
        self.id = id
        self.item = item
        self.amount = amount
    }
}

class Cart: Object {

    var items = List<CartItem>()

    func addItem(item: CatalogItem) {
        let realm = try! Realm()
        let newItem = CartItem(id: item.id, item: item, amount: 1)
        do {
            try realm.write {
                items.append(newItem)
            }
        } catch let e {
            handleError(e)
        }
    }
}

Error:

'RLMException', reason: 'Can't set primary key property 'id' to existing value '352'.'

I expect Realm to reference existing CatalogItem but it looks like it's trying to add another one. Is there a way to implement this kind of relationship? Note: all objects are stored in one realm.


Solution

  • As jpsim noted, the problem is that I try to append unmanaged object to Realm and there is an object with the same primary key in the database. I had to solve the problem differently because the proposed solution would erase properties of the object that exists in the realm.

    func addItem(item: CatalogItem) {
        let realm = try! Realm()
        var catalogItem = item
        if item.realm == nil,
            let foundItem = realm.objects(CatalogItem)
                .filter("id == %@", item.id).first {
            catalogItem = foundItem
        }
        let newItem = CartItem(id: item.id, item: catalogItem, amount: 1)
        do {
            try realm.write {
                items.append(newItem)
            }
        } catch let e {
            handleError(e)
        }
    }