I'm trying to pull locations near a user via Parse for tableview, except I only get either a white screen or an empty table.
Edited: I neglected to add the call for location before query near geopoint is submitted. In testing, I found that location was determined under 'geoPontForCurrentLocationInBackground'. However, once the geopoint is established and the query is submitted, the user location returns no latitude or longitude within the query. Moreover, the query doesn't return any objects and I'm not sure why:
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "User")
PFGeoPoint.geoPointForCurrentLocationInBackground {
(userLocation: PFGeoPoint!, error: NSError!) -> Void in
if error == nil {
}
}
let point: PFGeoPoint = PFGeoPoint(latitude: self.userLoc.latitude, longitude: self.userLoc.longitude)
query.whereKey("location", nearGeoPoint: point, withinMiles: 50.0)
query.limit = 10
return query
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as Shops
if let object = object {
if let shopName = object.valueForKey("shopName") as? String {
cell.shopList.text = shopName
}
}
return cell as Shops
}
I believe the issue is that Parse treats the User class as a special class. You may have difficulty querying User class if you treat it as if it was a Custom Class.
The correct and tested way to do this is to the the query function of PFUser.
var query = PFUser.query()
You are setting up the query, but never actually executing it. Per the Parse documents, you need to add:
// Final list of objects
queryResults = query.findObjects()
Before your queryForTable() return statement in order to get the query objects.
Related
I have an array of struct called displayStruct
struct displayStruct{
let price : String!
let Description : String!
}
I am reading data from firebase and add it to my array of struct called myPost which is initialize below
var myPost:[displayStruct] = []
I made a function to add the data from the database to my array of struct like this
func addDataToPostArray(){
let databaseRef = Database.database().reference()
databaseRef.child("Post").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let price = snapshotValue?["price"] as! String
let description = snapshotValue?["Description"] as! String
// print(description)
// print(price)
let postArr = displayStruct(price: price, Description: description)
self.myPost.append(postArr)
//if i print self.myPost.count i get the correct length
})
}
within this closure if I print myPost.count i get the correct length but outside this function if i print the length i get zero even thou i declare the array globally(I think)
I called this method inside viewDidLoad method
override func viewDidLoad() {
// setup after loading the view.
super.viewDidLoad()
addDataToPostArray()
print(myPeople.count) --> returns 0 for some reason
}
I want to use that length is my method below a fucntion of tableView
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myPost.count --> returns 0
}
Any help would be greatly appreciated!
You making a asynchronous network request inside closure and compiler doesn't wait for the response, so just Reload Table when get post data. replace the code with below it work works fine for you. All the best.
func addDataToPostArray(){
let databaseRef = Database.database().reference()
databaseRef.child("Post").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let price = snapshotValue?["price"] as! String
let description = snapshotValue?["Description"] as! String
// print(description)
// print(price)
let postArr = displayStruct(price: price, Description: description)
self.myPost.append(postArr)
print(self.myPost.count)
print(self.myPost)
self.tableView.reloadData()
//if i print self.myPost.count i get the correct length
})
}
Firebase observe call to the database is asynchronous which means when you are requesting for the value it might not be available as it might be in process of fetching it.
That's why your both of the queries to count returns 0 in viewDidLoad and DataSource delegeate method.
databaseRef.child("Post").queryOrderedByKey().observe(.childAdded, with: { // inside closure }
Inside the closure, the code has been already executed and so you have the values.
What you need to do is you need to reload your Datasource in main thread inside the closure.
databaseRef.child("Post").queryOrderedByKey().observe(.childAdded, with: {
// After adding to array
DispatchQueue.main.asyc {
self.tableView.reloadData()
}
}
So I am trying to change the background color for all rows in the chat, that a user has written himself. I do this by checking if the facebookID from the user, matches the facebookID on the message from the database.
But for some reason the .valueForKey on my Messages database keeps crashing.
var myUser:PFUser = PFUser.currentUser()!
var queryId = PFQuery(className: "Messages")
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.messageTableView.dequeueReusableCellWithIdentifier("messageCell") as! ChatTableViewCell
cell.textLabel?.text = messagesArray[indexPath.row]
let getUser = myUser.valueForKey("facebookID")
print(getUser)
let getIDUser = queryId.valueForKey("facebookID")
print(getIDUser)
//if getUser == getIDUser {
// make cell red
//}
return cell
}
This gives back the following in the console for getIDUser
reason: '[<PFQuery 0x133dadf10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key facebookID.'
So the getIDUser returns an error, but I don't have a clue why since the database and key do exist.
The problem is that this
var messageDB:PFObject = PFObject(className:"Messages")
creates just new message object. And you indeed can add it to the database.
But to get you objects from store you have to perform query.
Here are more info.
I am making an application where the user can see certain items/users within his x km radius (much like Tinder where you can set the radius of girls/guys you want to see in your area). So in my cellForRowAtIndexPath function I am determining whether a cell can be shown.
If he is in the radius, the event is shown. If the location is too far away, it shouldn't be using a cell.
My current code just hides the cell, but it is still clickable. I want it to NOT use a cell in the first place, but I couldn't find how to do it. Any ideas?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> AllGeneralMeetsTableViewCell {
//get location from backend
let locLati = object?["coordLat"] as? Double
let locLongi = object?["coordLong"] as? Double
let currentLocation:CLLocation = CLLocation(latitude: localLati , longitude: localLongi)
let meetLocation:CLLocation = CLLocation(latitude: locLati! , longitude: locLongi!)
let meters:CLLocationDistance = currentLocation.distanceFromLocation(meetLocation)
// make distance in km
let distInKm = meters/1000
//get distance that user has set in his settings
let cell = tableView.dequeueReusableCellWithIdentifier("GeneralMeetsIdentifier") as! AllGeneralMeetsTableViewCell!
if (distInKm <= settingsKm) {
// Extract values from the PFObject to display in the table cel
if let title = object?["title"] as? String {
cell?.titleCell?.text = title
}
if let message = object?["message"] as? String {
cell?.messageCell?.text = message
}
if let image = object?["image"] as? PFFile {
image.getDataInBackgroundWithBlock({ (imageData: NSData?,error: NSError?) -> Void in
if (error == nil) {
let image1 = UIImage(data:imageData!)
cell.imageCell.image = image1
}
})
}
return cell
}
else {
return cell
}
}
}
The data is being returned by the following query
override func queryForTable() -> PFQuery {
let query = PFQuery(className: self.parseClassName!)
let FBID = myUser.objectForKey("facebookID")!
query.whereKey("facebookID", equalTo: FBID)
query.whereKey("private", equalTo: "false")
return query
}
In your cellForRowAtIndexPath-
if cell is to be displayed {
tableView.rowHeight = 120
//Replace 120 with desired rowHeight
} else {
tableView.rowHeight = 0
}
Hope this helps :-)
You want to make the cell disappear when there is no data. If I am right, then you can use this library called as 'DZNEmptyDataSet' to display some images telling the user that there is no data to load. Use Cocoapods to install it, or just drag the files and create a bridging header. The usage is pretty straightforward as well - just follow the documentation at the GitHub.
The proper solution is to remove the data from your data source so that it won't be displayed in the first place. But sometimes this is a lot of work.
If you just want to hide a cell you can give it a zero height using the tableView(_:heightForRowAtIndexPath:) delegate method:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 1 {
return 0.0
}
return tableView.rowHeight
}
Parse provides a type PFGeopoint and it is even supported in the Query, with Parse framework you can also obtain location the same way you download stuff from database, this way you can download only those posts that are available for the user in his range...
PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil { //asynchronous, U have access to GPS location
// create Parse Query
let query: PFQuery = PFQuery(className: "Post")
// 5km range
query.whereKey("gps", nearGeoPoint: geoPoint, withinKilometers: 5.0)
//rest of query....
} else { // dont have access to GPS or whatever
print("Eror location: \(error)")
}
}
The first image is an example of what the result is returning and the second image is an example of the results page, 'distance' is the label that I need to change in order to display my users distance. I have all my users locations stored on Parse as PFGeoPoint called "location" in lat and long. I then have a tabelViewCell with a textLabel. All users are shown on the VC and I am trying to show how far these users are from the current user like in Tinder.
I have the other users locations running in the logs as lat and long coordinates and I have the text label updating from "distance" to "[] km away!" So I must be getting the array back but its returning empty.
I have searched the internet and can't seem to figure it out. All the tutorials are all obj c or json or to add annotations in mapView. Here is my code on my usersResultsViewController:
var locationManager : CLLocationManager!
var latitude: CLLocationDegrees = 0
var longitude: CLLocationDegrees = 0
#IBAction func done(sender: UIBarButtonItem) {
self.performSegueWithIdentifier("backToProfile", sender: self)
}
#IBOutlet var resultsPageTableView: UITableView!
var imageFiles = [PFFile]()
var instrumentText = [String]()
var nameText = [String]()
var ageText = [String]()
var locationText = [PFGeoPoint]()
var usersLocations = Double
let roundedTwoDigitDistance = Double
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
// start of tableView:
let query = PFQuery(className: "_User")
query.whereKey("username", notEqualTo:PFUser.currentUser()!.username!)
query.findObjectsInBackgroundWithBlock { (users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// success
print(users!.count)
for user in users! {
self.imageFiles.append(user["image"] as! PFFile)
self.instrumentText.append(user["instrument"] as! String)
self.nameText.append(user["name"] as! String)
self.ageText.append(user["age"] as! String)
// self.locationText.append(user["location"] as! PFGeoPoint)
}
// reload the table forget this it will load nothing
self.resultsPageTableView.reloadData()
} else {
print("error")
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// *** note to self: for the return here it must be a mandatory field for user look at this again nd change it to mandatory age or username or something.
return imageFiles.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let singleCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("mySingleCellid") as! CustomCell
// text
singleCell.usersInstrument.text = instrumentText[indexPath.row]
singleCell.userName.text = nameText[indexPath.row]
singleCell.userAge.text = ageText[indexPath.row]
let query = PFUser.query()!
if let latitude = PFUser.currentUser()?["location"]?.latitude {
if let longitude = PFUser.currentUser()?["location"]?.longitude {
print(latitude)
print(longitude)
query.whereKey("username", notEqualTo:PFUser.currentUser()!.username!)
query.whereKey("location", withinGeoBoxFromSouthwest: PFGeoPoint(latitude: latitude - 10, longitude: longitude - 10), toNortheast:PFGeoPoint(latitude:latitude + 10, longitude: longitude + 10))
query.findObjectsInBackgroundWithBlock { (users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// success
for user in users! {
singleCell.userDistance.text = "\(self.locationText) km away!"
here are some of the forums i have found helpful but I am still stuck!!!:
http://www.scriptscoop.com/t/a2d00e357960/ios-converting-a-pfgeopoint-lat-and-long-from-parse-into-a-cllocation-lat-.html
trying to access the subscript of a Parse query array in SWIFT
Two Query Constraints On One Key with Parse and Swift
PFGeopoints have methods called "distanceInMilesTo:" and "distanceInKilometersTo:". These are what you're going to want to use. Call that method on the PFGeopoint storing the current user's location, and pass in each user's location from your query. Store the result in the appropriate label.
Here is the link to the API reference for this method: http://parse.com/docs/ios/api/Classes/PFGeoPoint.html#//api/name/distanceInKilometersTo:
Use the "near" feature and start getting all users within, say, 1 mile, then save that information, then get all users within 2 miles, then 5 miles, etc. Continuing to put geo-fences around a specific location and growing in concentric circles with increasing radius/distance, you can save the estimated distance for each user.
It's also possible to query for the set of objects that are contained within a particular area. To find the objects in a rectangular bounding box, add the withinGeoBox restriction to your Parse.Query.
var query = new Parse.Query(PlaceObject);
query.withinMiles("location", userGeoPoint, 10.0);
query.find().then(function(placesObjects) {
// Get a list of objects within 10 miles of a user's location
});
More examples are at https://www.parse.com/docs/js/guide
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
var query = PFQuery(className:"category")
let object = objects[indexPath.row] as String
query.whereKey("type", equalTo:"DRUM")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
NSLog("%#", object.objectId)
let abc = object["link"]
println("the web is \(abc)")
cell.textLabel!.text = "\(abc)"
}
} else {
NSLog("Error: %# %#", error, error.userInfo!)
}
}
return cell
}
after add the let object = objects[indexPath.row] as String can't load the view, delete the line show only one row successfully.
First I advise you to get your cell data outside cellForRowAtIndexPath. This function is not a good place to receive data from parse. Make another function and create a class variable and put handle getting data from there.
let object = objects[indexPath.row] as String
for object in objects
Try not to use same variable names for different stuff, as they will confuse you.
This line is not contributing to anything at the moment it seems. Try deleting it:
let object = objects[indexPath.row] as String
First lets have principles in mind. Don't ever update UI from a separate thread, its behavior is unexpected or undefined. It works or works weird.
Second, the problem you have is the when the VC gets loaded the tableView's datasource is called there and then on the main thread. Now you tried to add something on the cell by doing a Async call in separate thread which will take time and main thread is not waiting when the call to parse is being done. If you have difficulty in Async please take a look at the documentation its really important to get a good grasp of the few terms and the principles.
The thing is your main thread runs top to bottom without waiting each call to server thats async in the cell generation. So the result of that call will post later on and you are not posting on main thread too.
Moreover, i would suggest you don't do this approach for big projects or manageable code base. I generally do is:
when the view loads call the Parse with the needed information
Wait for that on a computed variable which i will observe to reload table views once I'm conformed i have the data.
Initially table view will have 0 rows and thats fine. Ill make a spinner dance during that time.
I hope i made some issues clear. Hope it helps you. Cheers!
//a computed var that is initialized to empty array of string or anything you like
//we are observing the value of datas. Observer Pattern.
var datas = [String](){
didSet{
dispatch_async(dispatch_get_main_queue(), {
//we might be called from the parse block which executes in seperate thread
tableView.reloadData()
})
}
}
func viewDidLoad(){
super.viewDidLoad()
//call the parse to fetch the data and store in the above variable
//when this succeeds then the table will be reloaded automatically
getDataFromParse()
}
//get the data: make it specific to your needs
func getDataFromParse(){
var query = PFQuery(className:"category")
//let object = objects[indexPath.row] as String //where do you use this in this block
var tempHolder = [String]()
query.whereKey("type", equalTo:"DRUM")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
for object in objects!{
//dont forget to cast it to PFObject
let abc = (object as! PFObject).objectForKey("link") as? String ?? "" //or as! String
println("the web is \(abc)")
tempHolder.append(abc)
}
} else {
print("error") //do some checks here
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = datas[indexPath.row]
return cell
}