iosarraysswiftparse-platformpfquery

PFQuery returns Empty Array


Soo i have been adding information to parse server, the problem is i can't seem to get the Strings i need via PFQuery.

If I print the objects, it returns an empty Array. Also (fahrtenRequests.count = 0). It has been suggested that this is because that PFQuery can't read my data or my data is not in the right format.

Im completely lost here, it seems everything should work perfectly fine but doesnt

THANKS for your help in advance :) :)

My code:

import UIKit
import Parse

class HistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var zeit: Array = [String]()
var kosten:Array = [String]()
var kilometer: Array = [String]()
var datum: Array = [String]()


@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.delegate = self

tableView.register(UINib(nibName: "CellHistoryViewControllerTableViewCell", bundle: nil), forCellReuseIdentifier: "CellHistory")


let query = PFQuery(className: "GemachteFahrten")

query.whereKey("gefahreneZeit", equalTo: (PFUser.current()?.objectId!)!)

query.whereKey("kosten", equalTo: (PFUser.current()?.objectId!)!)

query.whereKey("createdAt", equalTo: (PFUser.current()?.objectId!)!)

query.whereKey("gefahreneKilometer", equalTo: (PFUser.current()?.objectId!)!)


query.findObjectsInBackground { (objects, error) in

    if error != nil {

        print("error finding user data")


    } else {

        print("check2")

        print(objects!)

        if let fahrtenRequests = objects {


            print(fahrtenRequests.count)

Thanks again for any help :)


Solution

  • No, that's not how PFQuery works. The object after equalTo: must be the same type as the object under the key:@"..." in your Parse dashboard.

    For example in your case, if you want to retrieve all the trips that one user made, you need to save the user as a pointer to the User class first:(In your parse dashboard, select Edit->Add a Column, then choose Pointer as the type, User as the target class)

    let trip = PFObject(className:"GemachteFahrten");
    //save the user who made this trip
    trip["user"] = PFUser.current()
    //save other things like costs, distance...etc
    trip["kosten"] = ...
    trip["gefahreneKilometer"] = ...
    trip.saveInBackground()
    

    Then, we want to get all the trips that belong to the current user:

    let query = PFQuery(className: "GemachteFahrten")
    query.whereKey("user", equalTo: PFUser.current())
    //then execute the query
    

    Please see the Parse documentation http://parseplatform.github.io/docs/ios/guide/#queries