I have a food ordering app that has an "order" tab with a table showing the items on the order and below the table, labels with the $ sub total and total. The order data is stored in a SQLite database.
I have a function named loadFromSQL
which reads the data into arrays that are used to populate the table cells and $ total labels.
I call loadFromSQL
from viewDidLoad
, viewDidAppear
and an @IBAction func didSwipeDown
. The table is refreshed when run from viewDidLoad
and the swipe-down but not from viewDidAppear
. I know the SQL is being read because the $ total labels are being updated even when called from viewDidAppear
.
I call self.tableView.reloadData
in DispatchQueue.main.async
right after updating the $ total labels at the end of loadFromSQL
.
Here are some code snippets:
@IBAction func didSwipeDown() {
loadFromSQL()
}
override func viewDidLoad() {
super.viewDidLoad()
loadFromSQL()
let nib = UINib(nibName: "OrderTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "OrderTableViewCell")
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
loadFromSQL()
}
This is from the end of loadFromSQL
:
sqlite3_finalize(selectStatementQuery)
//calculate sub total and total
dispOrderTotal = dispTotTax + dispSubTotal
let formattedTax = String(format: "$%.2f", dispTotTax)
let formattedSub = String(format: "$%.2f", dispSubTotal)
let formattedTotal = String(format: "$%.2f", dispOrderTotal)
self.orderTotalLab.text = formattedTotal
self.taxLab.text = formattedTax
self.subTotLab.text = formattedSub
DispatchQueue.main.async {
self.tableView.reloadData()
}
Any ideas would be greatly appreciated.