Yes. This is to be expected. The save method from MutablePersistableRecord may perform an insert (if the id is nil), and thus modify the book. You must declare the book with var:
var book: Book = ...
try book.save(db)
print(book.id) // guaranteed to be not nil
That's what the save method does (update or insert, depending on the presence of an id or not).
If you know your book already has an id, and is already present in the database, then prefer the update method, which will never mutate the book, and accepts let:
Yes. This is to be expected. The save method from MutablePersistableRecord may perform an insert (if the id is nil), and thus modify the book. You must declare the book with var :
var book: Book = ...
try book.save(db)
print(book.id) // guaranteed to be not nil
Thanks for this. It helps me.
The book was well declared as var in my view.
The error was in my saving function:
func saveBook(book: Book) throws {
try dbWriter.write { db in
try book.save(db)
}
}
=> This works with PersistableRecord, not MutablePersistableRecord.
It should be this, using inout keyword
func saveBook(_ book: inout Book) throws {
try dbWriter.write { db in
try book.save(db)
}
}
Then, the error message disappears in try book.save(db) line.
Yes, I used the demo app a lot to get inspired, but I have simplified the call to save() and it was a bad idea for my use case... (nned of mutable persistence)
Thanks for the replies in all cases.
More generally, if we have questions about good practices (doc read) for themes like App architecture with Combine, ValueObservation, what's the best place to post ? GitHub, StackOverflow, this forum ?
GitHub issues are a good place to ask questions about GRDB, the library, its demo apps, its documentation. Other questions should find their home on StackOverflow. App architecture questions fall in the StackOverflow bucket: the topic is very subjective, and GRDB is architecture-agnostic.