swift - How to update db data when moving an item in a list? - Stack Overflow

I am using swiftui to create a list and using .onMove to move items.I want to update the db information

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)")
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745586000a4634544.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信