PFQueryTableViewController not loading from Parse into Swift tableView

258 Views Asked by At

I am new to Swift and I am trying to find the current user location, and then query all nearby users and then load them into a UITableView in my storyboard. However, when I build my code, no data shows in my UITableView (Parse is my backend). I tried to research the problem and found this way of using PFQueryTableViewController:

PFQueryTableViewController in swift not loading data

However, when I do this

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

always returns an NSException error. How can I fix that problem or fix the below code to return my parse data into my table view?

import UIKit
import Parse
import ParseUI
import CoreLocation

class MasterTableViewController: PFQueryTableViewController {

var usersLocation: PFGeoPoint? {
    didSet {
        // This will reload the tableview when you set the users location.
        // Handy if you want to keep updating it.
        if (tableView != nil) {
            tableView.reloadData()
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    PFGeoPoint.geoPointForCurrentLocationInBackground { point, error in
        if error == nil {
            self.usersLocation = point
        }
    }

}

override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "User")
    if let location = usersLocation {
        query.whereKey("location", nearGeoPoint: location)
    }
    query.limit = 10
    return query
}
1

There are 1 best solutions below

2
On

In your queryForTable method it seems you're trying to query the User class.

From Parse docs, to query for users there is a special way of doing it:

example:

var query = PFUser.query()

query.whereKey("gender", equalTo:"female")

var girls = query.findObjects()

https://www.parse.com/docs/ios/guide#users-querying

Hopefully that helps