Dharma
Dharma

Reputation: 81

Swift 4 - DynamoDB data not showing up in TableView

I am trying to point an existing table view to the new DynamoDB database. The AWS DynamoDB call populates an array of dictionaries variable in tableview but the simulator is showing the data. I have spent several days trying with asynchronous function call with a completion closure without success. Now I got rid of the custom function and directly using the AWS closure in viewDidLoad() of table view. Any help is appreciated.

Here is the table view code:

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
    // ..
    }

}

Then, when I run the code, I add a print command in override tableview, but it is showing a blank array like [].

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

}

I tried reload of table view in multiple tries, but that didn't help either.

Here is the search feature having the isSearching variable

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

Upvotes: 2

Views: 205

Answers (1)

Osman
Osman

Reputation: 1586

ok tried to reload your data after you get your data, for example in your Closure.

You have to reload your data in your main thread like that :

 DispatchQueue.main.async {  
self.employee1View.reloadData()
}

Upvotes: 1

Related Questions