yam*_*mbo 2 database asynchronous uitableview ios
如何仅在从数据库加载数据后重新加载 UiTableView。我有一个数据源类,它包含加载所有数据的函数,并在将数据加载到数组中后存储数据。
当我使用 Tableview 访问数据时,虽然它不显示任何内容,因为 tableview 在数据库返回其数据之前加载。加载数据的唯一方法是让我有一个手动重新加载 tableview 的按钮。
从数据库填充数据源对象中的数组后,如何使 tableview 自动重新加载?
作为一名新程序员,我花了一段时间才知道这甚至是可能的,但我刚刚意识到完成块是要走的路。只有在您的函数完成从数据库中获取后,它才会执行一些指定的代码。在这种情况下,我可以在从数据库中检索数据后更新 tableview。这是一个例子。
函数定义:
func getDataFromDatabase(someInput: Any, completion: @escaping(String?) -> Void) {
let retrievedData: String = databaseCallFunction()
// 'Get data from database' code goes here
// In this example, the data received will be of type 'String?'
// Once you've got your data from the database, which in this case is a string
// Call your completion block as follows
completion(retrievedData)
}
Run Code Online (Sandbox Code Playgroud)
如何调用函数:
getDataFromDatabase(someInput: "Example", completion: { (data) in
// Do something with the 'data' which in this example is a string from the database
// Then the tableview can be updated
let tableView.title = data
tableView.reloadTableView
})
Run Code Online (Sandbox Code Playgroud)
完成块中的代码将在数据库调用完成后发生。