SQLiteData update selectedRow after insert #303
-
|
I have a PropertiesList model that has a number of observed properties, but in particular, I have this: when I insert a new property, I want to set the selectedProperty to the newly inserted property My confirmation action in my update form that has a Bindable instance of the PropertiesListModel ToolbarItem(placement: .confirmationAction) {
Button(role: .confirm) {
withErrorReporting {
try database.write { db in
try Property.upsert {
property
}
.execute(db)
let id = Property.ID(db.lastInsertedRowID)
model.selectedProperty = properties.first(where: {$0.id == id})
}
}
dismiss()
}
}The problem I am having is that the property gets inserted fine, and my properties list is refreshed, but the selecredProperty is not updated because in my ToolbarItem action, the id is set before the commit is complete. How can I delay that assignment until after the commit has completed? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @StewartLynch, there is a SQL tool called "RETURNING" clauses that allow you to get the data that was just inserted/updated into the database, and our query building library supports it like so: try database.write { db in
model.selectedProperty = try Property
.upsert { property }
.returning(\.self) // 👈
.fetchOne(db)
}That allows you to perform the database write and get the freshest value at the same time. And I'm not entirely sure I understand the problem you are having when you say " model.selectedProperty = try database.write { db in
try Property
.upsert { property }
.returning(\.self)
.fetchOne(db)
}I just pulled the |
Beta Was this translation helpful? Give feedback.
Hi @StewartLynch, there is a SQL tool called "RETURNING" clauses that allow you to get the data that was just inserted/updated into the database, and our query building library supports it like so:
That allows you to perform the database write and get the freshest value at the same time.
And I'm not entirely sure I understand the problem you are having when you say "
selectedPropertyis not updated", but if you only want to update theselectedPropertyfield of your model after the database transaction finishes, then you can do so like this:model.sel…