Search code examples
iosswiftuitableviewparse-platform

Swift Parse.com UITableView doesn't reloadData after updating the NSMutableArray


I have a UITableView that should display Users, it displays for the first time, but does not update when reloadData() is called.

The code initiating the tableView and the reloadData function are as follows:

let query = PFQuery(className: UserModel.parseClassName())
    query.whereKey("gender", equalTo: current.sexe)
    query.limit = countPerPage
    query.skip = pageIndex * countPerPage
    query.order(byAscending: "fullname")
    query.findObjectsInBackground(block: {(objects: [PFObject]!, error: Error!) -> Void in

        if error == nil {

            for object in objects as! [UserModel]{

                self.userArray.add(object)
            }

            self.serachTable.reloadData()

        } else {
            print("error")

        }
    })

Solution

  • reloadData must be called on main thread. Add DispatchQueue.main.async around reloadData:

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