The documentation states the obvious i.e.:
add(_:update:) Adds or updates an object to be persisted it in this Realm.
create(_:value:update:) Creates or updates an instance of this object and adds it to the Realm populating the object with the given value.
but I can't quite see the difference if any ?
add will update whole object at once, so there is a danger that you may miss an attribute. create can update partial information of the object by displaying attribute names.
Let's say cheeseBook is saved already as below.
let cheeseBook = Book()
cheeseBook.title = "cheese recipes"
cheeseBook.price = 9000
cheeseBook.id = 1
//update 1 - title will be empty
try! realm.write {
let cheeseBook = Book()
cheeseBook.price = 300
cheeseBook.id = 1
realm.add(cheeseBook, update:true)
}
//update2 - title will stay as cheese recipes
try! realm.write {
realm.create(Book.self, value:["id":1, "price":300], update:true)
}
add()
adds the passed-in object to the Realm (modifying the object to now refer to the data in the Realm), while create()
creates a copy of the object in the Realm and returns that copy, and does not modify the argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With