I have to query for a class called "Commitment" it has a pointer called "need". I need to be able to access the data in the pointer of need while querying Commitment. I have to query for Commitment because I have to check the "committer" object against the current user. Essentially I want to be able to see the needs that the current user has committed to.
Commitment Class:
Need Class:
let query = PFQuery(className: "Commitment")
query.order(byDescending: "createdAt")
query.whereKey("committer", equalTo: PFUser.current()!)
query.includeKey("need")
query.findObjectsInBackground {
(objects: [PFObject]?, error: Error?) -> Void in
if let objects = objects as [PFObject]? {
self.committment = []
self.commitObject = []
for object in objects {
self.committment.append(Committment(id: object.objectId!, needName: object["needName"] as! String))
self.commitObject.append(object)
}
self.tableView.reloadData()
} else {
print("failure")
}
}
This is my current query. It returns nil on needName so obviously that isn't working. Any help is appreciated.
The reason that you get nil is because you are not accessing the need object but you are trying to get it from the Commitment object
just change your code to something like this:
let query = PFQuery(className: "Commitment")
query.order(byDescending: "createdAt")
query.whereKey("committer", equalTo: PFUser.current()!)
query.includeKey("need")
query.findObjectsInBackground {
(objects: [PFObject]?, error: Error?) -> Void in
if let objects = objects as [PFObject]? {
self.committment = []
self.commitObject = []
for object in objects {
let need = object["need"] as [PFObject]?
self.committment.append(Committment(id: object.objectId!, needName: need["needName"] as! String))
self.commitObject.append(object)
}
self.tableView.reloadData()
} else {
print("failure")
}
}
Notice that i first access the need object and from there extract the needName property.
BTW! i am not sure that the syntax is 100% accurate (i wrote it in freestyle) but i am sure that you got the idea..
Related
I want to retrieve these objects and store them in an array.
When I print inside the function, everything works fine. However, printing outside, the arrays are empty.
I've tried without the function and followed an answer on Stack Overflow but still no idea what's going on.
override func viewDidLoad() {
super.viewDidLoad()
func getAllInfoFromParse () {
let query = PFQuery(className:"UserQRCodes")
query.whereKey("userName", equalTo: PFUser.current()!)
query.findObjectsInBackground { (results, error) in
if let error = error {
print(error.localizedDescription)
} else if let results = results {
for object in results {
if let userInfo = object["info"] as? String {
self.infoTakenFromUsersParse.append(userInfo)
print(self.infoTakenFromUsersParse)
}
}
}
}
}
getAllInfoFromParse ()
print(self.infoTakenFromUsersParse)
self.infoTakenFromUsersParseWithoutDups = self.infoTakenFromUsersParse.removingDuplicates()
print("The info from parse is here \(self.infoTakenFromUsersParseWithoutDups)")
print("The info from parse is here \(self.infoTakenFromUsersParse)")
print(self.imageFiles)
print(imageFiles)
print(imageFiles)
Console:
logged in
[]
The info from parse is here []
The info from parse is here []
[]
[]
[]
[]
["Coffee Shop"]
The array is to populate a tableView. What am I doing wrong?
You don't have to query for each individual item again. Those objects should be in the original response. You also need to make sure that the username that you are querying for on line 3 is for the actual user's username and not the actual user object. Lastly, based on your comments, it appears that you need some kind of callback function to tell when the request has finished executing so add that to the getInfoFromParse function. Try something like this:
func getInfoFromParse(callback: () -> Void) {
let query = PFQuery(className:"UserQRCodes")
query.whereKey("userName", equalTo: PFUser.current()!)
query.findObjectsInBackground { (results, error) in
if let error = error {
print(error.localizedDescription)
} else if let results = results {
for object in results {
if let userInfo = object["info"] as? String {
self.infoTakenFromUsersParse.append(userInfo)
}
}
self.myTableView.reloadData()
callback()
}
}
}
EDIT: Then, when you're ready to access the results of this function do something like this:
getInfoFromParse {
print(self.infoTakenFromUsersParse)
}
I have searched far and wide for an answer to this. In written English I'm trying to do the following:
Query results where the column "WorkGroup" in the "Issues" class is equal to the column "WorkGroup" in the "Users" class.
I tried countless things including; NotContainedIn, ObjectForKey, wherekey (matchesquery), includeKey, add pointers, etc.
let query = PFQuery(className: ISSUES_CLASS_NAME)
let now = Date()
query.whereKey(ISSUES_SUB_DATE, lessThanOrEqualTo: now)
query.whereKey(ISSUES_STATUS, notEqualTo: "Closed")
query.order(byDescending: ISSUES_SUB_DATE)
// Add Where Issues WorkGroup value = Users WorkGroup value.
let userQuery = PFUser.query();
// Set up user query
let issueQuery = PFQuery(className:"Issue");
issueQuery.whereKey("WorkGroup", matchesKey:"WorkGroup", inQuery:userQuery);
http://parseplatform.org/Parse-SDK-iOS-OSX/api/Classes/PFQuery.html#/c:objc(cs)PFQuery(im)whereKey:matchesKey:inQuery:
Try this
func yes(){
let query = PFUser.query()
query?.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil{
for object in objects!{
self.array.insert(object.object(forKey: WorkGroup) as! String, at: 0)
let dataQuery = PFQuery(className: "Issues")
dataQuery.whereKey(WorkGroup, containedIn: self.array)
dataQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil{
for object in objects!{
}
}
})
}
}
})
}
Double check the brackets
I am trying to query for all of the existing users in my app that the user has not added as a "Friend". I am getting the error
Cannot do a comparison query for type: PFRelation
Here is my current code:
override func queryForTable() -> PFQuery {
let currentFriends : PFRelation = PFUser.currentUser()!.relationForKey("friends")
// Start the query object
let query = PFQuery(className: "_User")
query.whereKey("username", notEqualTo: currentFriends)
// Add a where clause if there is a search criteria
if UserInput.text != "" {
query.whereKey("username", containsString: UserInput.text)
}
// Order the results
query.orderByAscending("username")
// Return the qwuery object
return query
}
How do I solve this issue? Thanks
I solved my problem.... in a way. I was unable to compare the PFRelation so I created a helper Parse class that saves the user who adds the other user as "from" and the user they add as "to" then I am able to compare them like this.
let currentUser = PFUser.currentUser()?.username
let currentFriends = PFQuery(className: "Friends")
currentFriends.whereKey("from", equalTo: currentUser!)
// Start the query object
let query = PFQuery(className: "_User")
query.whereKey("username", doesNotMatchKey: "to", inQuery: currentFriends)
I hope this helps someone in the future.
In the app Im working on, users select a set of "tags" among a list, then I need to query all items which include at least one of those tags.
To do so, I want to use compound queries.
documentation from Parse.com
var lotsOfWins = PFQuery(className:"Player")
lotsOfWins.whereKey("wins", greaterThan:150)
var fewWins = PFQuery(className:"Player")
fewWins.whereKey("wins", lessThan:5)
var query = PFQuery.orQueryWithSubqueries([lotsOfWins, fewWins])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
my issue here is that my tags are in an array, and I don't know how much there is (could be 1, could be 3). So I though about doing a for loop to create a query for every one of them.
my problem is that I can't access the variable outside of this for loop, and I can't declare them before hand as I don't know how much there will be.
something that would look like this.
for(var i = 0; i < tags.count; i++){
let i = PFQuery(className: "items")
i.whereKey("tags", equalTo: tags[i])
}
var query = PFQuery.orQueryWithSubqueries([variable1, variable2 ... ])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results
}
}
is there a way to achieve such thing?
I have an app that I need to grab all values in the class. I need to get "players" and "total" from the class "runningTotal". Here is the code I have:
var query = PFQuery(className:"runningTotal")
query.selectKeys(["players", "total"])
query.findObjectsInBackgroundWithBlock
{
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil
{
self.test = objects[0]["total"]
}
}
I want to set a variable (test) equal to the result of total. I would also want to do this with players. I don't think the above code is right, as it doesn't work. I obviously don't need any constraints as I want to fetch all of the results from this class. How would I go about solving this?
Thanks for any help in advance!
As long as your query is error free, you'll need to iterate through the objects array. As you iterate through each object, which will be of type AnyObject, you will need to cast the object as a PFObject. Then you will be able to grab the data you require from it.
var query = PFQuery(className:"runningTotal")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil{
for object in objects{
if let data = object as! PFObject{
//Set test to total (assuming self.test is Int)
self.test = data["total"] as! Int
}
}
}else{
//Handle error
}
}