objectbox / objectbox-swift

Swift database - fast, simple and lightweight (iOS, macOS)
https://swift.objectbox.io
Apache License 2.0
465 stars 30 forks source link

Update a record cause duplicate of data #18

Closed daoan1412 closed 5 years ago

daoan1412 commented 5 years ago
// objectbox: entity
struct Author {
    let id: Id
    let name: String
    let age: Int
}

let exampleEntityBox = store.box(for: Author.self)
var exampleEntity = Author(id: 0, name: "Nnedi Okorafor", age: 45)

exampleEntity.id = try exampleEntityBox.put(exampleEntity)

// update record
exampleEntity.age = 50
try exampleEntityBox.put(exampleEntity)

exampleEntityBox.subscribe { records, _ in
      //--> duplicate records
}
Environment:
Xcode 11 GM seed 2
ObjectBox: 1.0.0-rc.10
uli-objectbox commented 5 years ago

@daoan1412, thank you for contacting us! This works for me.

How are you creating your store, and have you checked that the object wasn't already in the store from a previous run? What happens when you use "\(Date())" for the name? Do the duplicates have the same date and time?

I had to modify your code slightly to make it compile. id and age arelet constants, so the compiler will complain about the assignments to them, and you're ignoring the return value from subscribe().

Once I changed id and name to var and added a _ = before the subscribe (in a real program, you'd of course keep the subscription around in a property or so), the code worked, and the records array contains only the single object I wrote.

daoan1412 commented 5 years ago

Sorry, this is my mistake.