0
votes

I am adding my item to realm using realm.add(item, update: true) and the realm object is created globally - var realm = Realm(), when I add few item continuously it gets replaced:

Let us assume I have 3 items and I add 1 it becomes 4, later again when I add one more still the count will be...after this even tough I add items it doesn't get increased.

I do add operation inside write block, realm.write. But when I relaunch my app I get all the items. Not sure what is going wrong. I tried even refreshing, but it didn't help.

1

1 Answers

2
votes

This happened to me too. For me, using primary key solved it:

dynamic var primaryKeyID: String = ""

override class func primaryKey() -> String {
    return "primaryKeyID"
}

override init() {
    super.init()
}

init(id: String) {
    super.init()

    self.primaryKeyID = id
}

And of course generating it like this:

let customRealmObejct = CustomRealmObject(id: NSUUID().UUIDString)

EDIT:

If you don't want to replace, you have to set update to false. Otherwise you'll get rewrites!

realm.add(item, update: false)