Reputation: 11
I'm building an app to help people carpool from the airport and I'm trying to query my database for rides within a certain range.
Here is what I have:
import UIKit
import Firebase
class DiscoverRideViewController: UIViewController {
var userFullName = String()
var dateEarly = Constants.UserInfo.chosenDate.addingTimeInterval(-900)
var offset = TimeInterval()
@IBOutlet weak var createRide: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print(dateEarly)
offset = TimeInterval(TimeZone.current.secondsFromGMT())
print("timezone offset: ", offset)
dateEarly = dateEarly.addingTimeInterval(offset)
print("searching for documents:")
let db = Firestore.firestore()
db.collection("rides-wfu").whereField("aiport", isEqualTo: Constants.UserInfo.chosenAirport).whereField("ridetime", isGreaterThanOrEqualTo: dateEarly).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
}
@IBAction func newRideTapped(_ sender: Any) {
let db = Firestore.firestore()
print(Constants.UserInfo.chosenDate)
db.collection("rides-wfu").addDocument(data: ["airport":Constants.UserInfo.chosenAirport, "ridetime":Constants.UserInfo.chosenDate, "groupmembers":[Constants.UserInfo.firstName+" "+Constants.UserInfo.lastName, "", "", ""]])
}
}
The create ride function is successful, but I am having trouble querying, and yes, I have built an index. I've tried just about everything and db.collection().where() methods are creating errors on my compiler for some reason -- only whereField() methods are working. Any help or guidance would be appreciated.
Constants class: My constants class seems to be working it is how I am sharing variables between view controllers.
This is the error I receive when I try to use 'where' instead of 'whereField', although from the documentation it looks like 'where' is a valid command for querying the Firestore database. Could it be that I need to download any additional pods? The ones I have are: pod 'Firebase/Analytics' pod 'Firebase/Auth' pod 'Firebase/Core' pod 'Firebase/Firestore'
Upvotes: 1
Views: 912
Reputation: 600071
You now have this as a query:
db.collection("rides-wfu").where("ridetime", ">", timestampEarly)
This is not a valid query syntax for Swift. What you're looking for is:
db.collection("rides-wfu").whereField("ridetime", isGreaterThanOrEqualTo: timestampEarly)
I highly recommend studying the Firebase documentation on querying Firestore, as that contains examples of this and many more types of queries.
Upvotes: 1