I am using parse in my app and I want to satisfy the two query and return object without using orQueryWithSubqueries. Here my query to parse code:
func queryToParse(){
var queryForBlood = PFQuery(className: "Donors")
queryForBlood.whereKey("BloodGroup", equalTo: bloodGroupTextField.text)
var queryForCity = PFQuery(className: "Donors")
queryForCity.whereKey("City", equalTo: citySearchTextField.text)
var query = PFQuery.orQueryWithSubqueries([queryForCity,])
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
self.tableData = objects as NSArray
println(self.tableData)
self.tableView.reloadData()
}
else
{
println(error)
}
}
}
Instead of creating two separate PFQuery, then you just have to create one. You only need to create several PFQuery when you want to make an OR query.
Your code should look something like this:
func queryToParse(){
let query = PFQuery(className: "Donors").whereKey("BloodGroup", equalTo: bloodGroupTextField.text).whereKey("City", equalTo: citySearchTextField.text)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
...
}
}
Related
I migrated to Swift 3 and have updated all my Parse functions to the latest syntax. Now, none of the queries return anything. There is no no error but there are also no objects. Whats weird is that it doesn't look like its even making a call, as it instantly returns no objects and there is no activity indicator spinning in the status bar like usual. Here's the query code:
let profileQuery:PFQuery = PFQuery(className: "_User")
profileQuery.whereKey("emailLowercase", equalTo: emailField.text!.lowercased() as String)
profileQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
print(objects?.count)
})
Thanks!
Try it as such:
let Query = PFQuery(className: "_User")
Query.whereKey("emailLowercase", equalTo: emailField.text!.lowercased() as String)
Query.findObjectsInBackground(block: { (objects, error) -> Void in
if (error == nil) {
print("Success")
print(objects?.count)
} else {
print("Error")
}
})
Your query code should be like this;
let query = PFUser.query()
The only thing wrong with your as Baris has suggest is that you are querying the class wrong. You can't query with ("_User")
let profileQuery = PFUser.query()
profileQuery.whereKey("emailLowercase", equalTo: emailField.text!.lowercased() as String)
profileQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil{
print(objects?.count)
}
})
i have 3 classes, i need to retrieve data depending on some condition , i use this code :
let query = PFQuery(className: "RiderRequest")
query.whereKey("username", equalTo: (PFUser.current()?.username!)!)
query.findObjectsInBackground(block: { (objects, error) in
if let riderRequests = objects {
for riderRequest in riderRequests {
if let driverUsername = riderRequest["driverResponded"] {
let query3 = PFQuery(className: "User")
query3.whereKey("username", equalTo: driverUsername)
query3.findObjectsInBackground(block: { (objects, error) in
if let driverinfo = objects {
for driver in driverinfo {
print("driverobject=\(driver)")
}
}
})
but the className: "User" not working and cant get data from it , always the object is nil.
Another way to retrieve data from the User Class with PFQuery is using the following:
let query : PFQuery = PFQuery(className: "_User")
Parse User is a special Parse class and requires a different Query constructor.
Update your code to:
let query3 = PFUser.query()
I need to do two things in the same Parse query. 1) I need to find the total number of objects returned by the given query; and 2) Only display the first 20 objects. I can't do both by setting query.limit = 20because the total number of objects will only be 20. If the total number of objects is 100, I need to get that number.
So, how can I progammatically display only the first 20 objects while still receiving all 100?
var query = PFQuery(className: "Professions")
query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
query.orderByDescending("createdAt")
// query.limit = 20
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
// I tried using something like:
// for var i = 0; i <= 20; i++ {
// if object[i] {
// But get 'Int' is not convertible to 'String'
if let title = object["title"] as? String {
println(title)
}
}
}
} else {
println(error)
}
})
When I try setting the following, I always get fatal error: array index out of range.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
Maybe not the most elegant solution, but i think you need to do two querys on the same query. One for the object.count and one with query.limit.
var query = PFQuery(className: "Professions")
query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
var numberOfObjects = objecs.count
}
else {
println(error)
}
query.limit = 20
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
if let title = object["title"] as? String {
println(title)
}
}
}
else {
println(error)
}
}
})
First, if you just need to count objects, Parse has a method for that:
query.countObjectsInBackgroundWithBlock
Then you can issue another PFQuery to get the first 20 objects. Fetching all the objects just to count them locally is bad design.
Nonetheless, if you still have good reason to retrieve all objects (limited by Parse at 1000) and process them locally, getting the first 20 is not done with Parse, it's done in Swift, locally, after you have fetched all objects.
var fetchedProfessions = [PFObject]()
var query = PFQuery(className: "Professions")
query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
query.orderByDescending("createdAt")
query.limit = 100
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
// Capture your results
self.fetchedProfessions = objects
}
} else {
print(error)
}
})
// Get the first 20
let firstTwentyProfessions = retrievedObjects[0..19]
When I run this query:
var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.orderByAscending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in ........
The results are never sorted by data, in fact they are always sorted by objectId.
What is wrong in my query?
This is my data on parse, sorted by date
This is my output on the device, sorted by objectId...
Had the same issue. Try adding this to your above code in block, it worked for me:
let array:NSArray = self.queryobjectsArray.reverseObjectEnumerator().allObjects
self.queryobjectsArray = NSMutableArray(array: array)
This whole issue ended quite strange...
What I did before, like in other views within the same project, I fetched the data from parse and pinned it to the local storage and called a second method to fetch the data from the local storage. This way I have fast reachable data from local storage and update it in the background for later use.
For some reason, the fetched data from online was sorted by date.
But the fetched data from the local pin was not and that was the reason, why my app displays the data in the wrong timeline.
I was not able to understand or solve this, but I helped myself by removing the local pinning and just grab the data from the internet.
I will show what the original process was:
var cardSetObjects: NSMutableArray! = NSMutableArray()
override func viewDidAppear(animated: Bool) {
self.fetchAllObjectsFromLocalDatastore()
self.fetchAllObjects()
}
func fetchAllObjectsFromLocalDatastore(){
var query = PFQuery(className: "CardSet")
query.fromLocalDatastore()
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil){
var temp: NSArray = objects as NSArray
self.cardSetObjects = temp.mutableCopy() as NSMutableArray
self.tableView.reloadData()
}else{
println(error.userInfo)
}
}
}
func fetchAllObjects(){
var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil) {
PFObject.pinAllInBackground(objects, withName:"CardSet", block: nil)
self.fetchAllObjectsFromLocalDatastore()
self.tableView.reloadData()
}else{
println(error.userInfo)
}
}
}
What I did at the end was to remove the fetchAllObjectsFromLocalDatastore method and put the data into the NSMutableArray right in fetchAllObjects.
But I still wonder why I had this sorting issue with this code...
Maybe someone will know...
Hi my query is very simple.
I basically want to return the objects ordered by the distance from the user.
If I pass in a PFGeoPoint, the query will return nothing. There are no errors.
If I don't put in a PFGeoPoint the query returns objects.
Whats going on?
Thanks :)
var query = PFQuery(className: "resorts")
query.whereKey("geoPoint", nearGeoPoint: loc)
query.limit = 100
query.whereKey("showMe", equalTo: true)
query.findObjectsInBackgroundWithBlock { (objects, error : NSError!) -> Void in
if error == nil
{
self.objects = objects as [PFObject]
}
} //returns nothing
var query = PFQuery(className: "resorts")
query.whereKey("geoPoint", nearGeoPoint: loc)
query.limit = 100
query.whereKey("showMe", equalTo: true)
query.findObjectsInBackgroundWithBlock { (objects, error : NSError!) -> Void in
if error == nil
{
self.objects = objects as [PFObject]
}
} //returns objects
try this
var myList = [AnyObject]()
query.findObjectsInBackgroundWithBlock{(object:[AnyObject]!, error = NSError! ) -> Void in
if error == nil {
println("successful query")
for object in objects {
self.myList.append(object)
}
} else {
println(error)
}
}