admin管理员组文章数量:1432359
I am using swiftui to create a list and using .onMove to move items. I want to update the db information stored in sqlite when moving items. I am using the stephencelis/SQLite.swift library for sqlite.
I have created the code as below. But the code below updates the entire list after the item is moved. Is this method efficient? Or is there another way? For example, updating the moved item and the item to the destination.
Can I get a better code that updates the db of sqlite when moving items in the list? Thank you.
@State private var listItem = [Item]()
ForEach(listItem) { item in
ListCell(
item: item,
onDelete: { deleteItem(item) }
)
}
.onMove(perform: moveItem)
func moveItem(from source : IndexSet, to destination : Int)
{
var tempDataSet : [Item] = listItem
listItem.move(fromOffsets : source, toOffset : destination)
for (index, item) in listItem.enumerated()
{
dbManager.updateDbData(listItem[index].date, listItem[index].content, listItem[index].amount, tempDataSet[index].id)
}
listItem = dbManager.loadDbData()
}
func updateDbData(_ date : String, _ content : String, _ amount : String, _ id : Int)
{
do
{
let update = MainTable.filter(dbId == id)
try db.run(update.update(dbContent < -content, dbDate < -date, dbAmount < -amount))
}
catch (let err)
{
print("updateDbData err = \(err)")
}
}
本文标签: swiftHow to update db data when moving an item in a listStack Overflow
版权声明:本文标题:swift - How to update db data when moving an item in a list? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745586000a2664910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论