我试图把现有的表视图指向新的DyDoDB数据库。aws dynamodb调用在tableview中填充一个字典变量数组,但是模拟器正在显示数据。我花了几天时间尝试使用带有完成闭包的异步函数调用,但没有成功。现在,我去掉了自定义函数,直接使用表视图的viewdidload()中的aws闭包。如有任何帮助,我们将不胜感激。
下面是表视图代码:
override func viewDidLoad() {
super.viewDidLoad()
//dynamodb call
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
dynamoDBObjectMapper.scan(Employees.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
}
let paginatedOutput = task.result!
for emp in paginatedOutput.items as! [Employees] {
self.myVariables.empDict["empid"] = emp._empid
self.myVariables.empDict["email"] = emp._email
self.myVariables.empDict["firstname"] = emp._firstname
self.myVariables.empDict["lastname"] = emp._lastname
self.myVariables.empDict["location"] = emp._location
self.myVariables.empDict["mobile"] = emp._mobile
self.myVariables.empDict["work"] = emp._work
self.myVariables.empDict["site"] = emp._site
self.myVariables.arrayEmployees.append(self.myVariables.empDict)
//print(self.myVariables.arrayEmployees) // this works
} // for loop
self.employeeSearch = self.myVariables.arrayEmployees
print("printing employeeSearch")
print(self.employeeSearch) // This works
// self.employee1View.reloadData()
// tried reloading here (above - showing here as commented), but getting error: UITableView.reloadData() must be used from main thread only
return nil
} // dynamoDBObjectMapper.scan
// self.employee1View.reloadData()
// Then I tried reload above (showing here as commented), but I get error : Expected ',' separator. If I accept compiler's suggestion, it puts a comma just after curly braces above and that causes more errors at the Line dynamoDBObjectMapper.scan error : Cannot invoke 'continueWith' with an argument list of type '(block: (AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any?, Void)'
) // .continueWith
// end dynamodb call
// other things in this overload function
// ..
}
}
然后,当我运行代码时,我在override tableview中添加了一个print命令,但是它显示了一个类似于[]的空白数组。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("printing employeSearch from numberOfRowsInSection")
print(employeeSearch) // This returns blank like []
if isSearching {
return currentEmployeeSearch.count
}
return employeeSearch.count
}
我多次尝试重新加载表视图,但也没有帮助。
下面是具有ISSearching变量的搜索特性
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
employee1View.reloadData()
} else {
isSearching = true
employeeSearch = empListDict // getSwiftArrayFromPlist()
currentEmployeeSearch = employeeSearch.filter {($0["lastname"]?.lowercased().contains(searchText.lowercased())) ?? false}
employee1View.reloadData()
}
}