Parse get all the objects from a pointer column - ios

I want to get all the objects in a pointer column from a query. I have a object class named A that contains a pointer column to object B. (Only 1 object) I have a query for A and I want to get all the B objects from that query. So I'm trying to do something like this...
var queryA = PFQuery(classname: "A");
queryA.whereKey("level", equalTo: 1);
var queryB = PFQuery(classname: "B);// not sure if this is necessary
//here I couldn't find anything, this should be something like
//get objects from queryA's "objectB" column
//After merging the column key with queryB (assuming I can)
queryB.whereKey("games", lessThan: 5); //I add constraints
When I try to use queryB.whereKey("key", matchesKey: "objectB", inQuery: queryA) it doesn't work because the "objectB" key points to the B objects not a key in it. Do you guys have any ideas? (Any answer is greatly appreciated. I can understand Objective-C as well as Swift)

You can do this with the includeKey() method. It will get the related data for the pointer column.
var queryA = PFQuery(classname: "A");
queryA.whereKey("level", equalTo: 1);
query.includeKey("columnName");
The second query (queryB) is not necessary.

Related

Compare and Match Array Values

When the user searches for a category I need all the arrays that contain that same category to appear + the other categories that are in that respective array.
Once the user has chosen ["Apples", "Oranges", "Limes"] I want to compare which array (out of many) that I queried contains Apples, Oranges or Limes. This can be one array or this can be many arrays.
These are the arrays I'm adding the values to:
var categoryNeeded = [AnyObject]() //The user creates this one and adds values to it
var categoryArr = [AnyObject]() //The Parse arrays are added here:
I have a simple Parse query function.
var query : PFQuery = PFUser.query()!
query.whereKey("contacts", containsString: "\(categoryArr)")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects as [PFObject]! {
for object in objects {
self.categoryArr.append(object["contacts"] as! AnyObject)
print(self.categoryArr)
}
}
}
The 2nd line is suspect:
query.whereKey("contacts", containsString: "\(categoryArr)")
When querying with that line, I get this error (without a crash):
2016-01-23 15:53:47.508 CC[28514:5733236] [Error]: $regex only works
on string fields (Code: 102, Version: 1.11.0)
Without the whereKey line, I get all the values and it prints them. I just can't figure out how to compare and check for matches between the two arrays which ultimately gives the matching arrays. Is there a Swift method that does that?
You should not use containsString but rather containedIn:
query.whereKey("contacts", containedIn: categoryArr)

Count strings inside array

This is my parse.com "currentUploads" class:
Image here
How can I count the amount of strings inside the array? As you can see on the first row, there is 1, and on the last one, it is 3. How can I return this count in a println? I have tried this:
var query = PFQuery(className:"currentUploads")
query.whereKey("objectId", equalTo: post.objID)
query.countObjectsInBackgroundWithBlock {
(count: Int32, error: NSError?) -> Void in
if error == nil {
print("The number is \(count) in total")
}
}
I know there is missing something using the "likedBy", but I dont know where to do what. Any ideas please?
The best way to count all the entries in an array column across all instances (rows) is to iterate the instances in cloud code and count each. This is done simply using the each query and summing up the length of each array then returning the result.
A similar thing can be done on the client, but you'll need to deal with pagination yourself as PFQuery doesn't offer each. In this case you would iterate the objects in each query response and sum the count of each array.

PFQueryTableViewController - Getting different objects

I've two Parse tables, Users and Evaluations that are connected by a pointer reference to User.ref (a pointer to itself) -> Evaluations.evaluatedId (a user pointer)
In my PFQueryTableViewController I need to get all users list with their last evaluation. I'm trying with:
override func queryForTable() -> PFQuery
{
let userQuery = PFUser.query()!
let evaluationQuery = PFQuery(className: "Evaluations")
evaluationQuery.whereKey("evaluatorId", matchesKey: "ref", inQuery: userQuery)
let query = PFQuery.orQueryWithSubqueries([userQuery, evaluationQuery])
query.cachePolicy = PFCachePolicy.CacheThenNetwork
return query
}
But I'm getting this error:
'All sub queries of an or query should be on the same class.'
I've searched for a while and I found this:
When using orQueryWithSubqueries, each subquery should be for the same class. If you need to query different classes, you will need either a separate query for each, or use whereKey:matchesQuery: if the other class is pointed to by the parent class.
Any suggestions? Thanks in advance!

How to get a query retrieving two objects from Parse via Swift

I have a parse database as in the picture.
I need to retrieve with a query code, two objects, for example the last created dates of type 2 and 3.
I am trying the codes below, but dont know how to merge these two queries (query2 and query3)? Or might there be another way to retrieve these two objects as one table?
var query2 = PFQuery(className: "stories")
query2.whereKey("Type", equalTo: 2)
query2.addDescendingOrder("createdAt")
query2.getFirstObject()
var query3 = PFQuery(className: "stories")
query3.whereKey("Type", equalTo: 3)
query3.addDescendingOrder("createdAt")
query3.getFirstObject()
I don't think you could do exactly what you currently have with 1 query. You could combine them but only to get back an array of all 2 and 3 types and then split them out yourself.
If the issue is making 2 different network requests then you could create a cloud code function to run the 2 queries and return the results in a single response.
you can do this one query as well
var query2 = PFQuery(className: "stories")
query2.whereKey("Type", equalTo: 2)
query2.whereKey("Type", equalTo: 3)
query2.addDescendingOrder("createdAt")
You can change you approach too , you can send array to compare and sort results
query2.whereKey("Type", containedIn: #[2,3])
query2.addDescendingOrder("createdAt")
After this don't use getFirstObject ; get all array and get your desired results using predicate or any else , this will save one network call as well.
This is for getting 2 values from Array
NSArray *fullArray= All objects from the parse ;
NSMutableArray *selectedObjectsArray = [NSMutableArray array];
for(int i=0 ; i<fullArray.count ; i++){
if (selectedObjectsArray.count==1 && ![[[selectedObjectsArray objectAtIndex:0] objcetForKey:#"type"] isEqualToString:[[Full array objectAtIndex:i] objcetForKey:#"type"]]) {
[selectedObjectsArray addObject:[fullArray objectAtIndex]];
break ;
}else{
[selectedObjectsArray addObject:[fullArray objectAtIndex]];
}
}
In Swift (i m not too good in swift so double check swift code before using)
for object in fullArray {
if (selectedObjectsArray.count==1 && ![[selectedObjectsArray[0][#"type"] isEqualToString:fullArray[i][#"type"]) {
selectedObjectsArray[1]=[fullArray[i];
break ;
}else{
selectedObjectsArray[0]=[fullArray[i];
}
}

Parse.com PFUser Query based on results from PFObject query

I have a chatroom class containing users. I can successfully query that class and return only specific PFUsers.
// get all room IDs associated with current user
var outerRoomQuery = PFQuery(className:Parse.CHATROOM_CLASS_NAME)
outerRoomQuery.whereKey(Parse.CHATROOM_USER, equalTo: PFUser.currentUser()!)
outerRoomQuery.selectKeys([Parse.CHATROOM_ROOMID])
// get all users associated with above roomIds ignoring current User
var innerRoomQuery = PFQuery(className:Parse.CHATROOM_CLASS_NAME)
innerRoomQuery.whereKey(Parse.CHATROOM_ROOMID, matchesKey:Parse.CHATROOM_ROOMID, inQuery: outerRoomQuery)
innerRoomQuery.whereKey(Parse.CHATROOM_USER, notEqualTo: PFUser.currentUser()!)
innerRoomQuery.includeKey(Parse.CHATROOM_USER)
innerRoomQuery.orderByDescending(Parse.CHATROOM_UPDATEDACTION)
innerRoomQuery.selectKeys([Parse.CHATROOM_USER])
The above query returns about 5 users.
I'm wondering if it is possible to combine these results with a separate PFUser.query() to create an OR query something like this:
var userQuery = PFUser.query()
userQuery?.whereKey(Parse.CHATROOM_USER, matchesKey:Parse.CHATROOM_USER, inQuery: innerRoomQuery) // returns nil
var receptionQuery = PFUser.query()
receptionQuery?.whereKey(Parse.USER_ROLE, equalTo:Parse.PFROLE_RECEPTIONIST) // returns around 10 receptionist users
var orUserQuery = PFUser.query()
return PFQuery.orQueryWithSubqueries([userQuery!, receptionQuery!])
The result of above only returns the receptionists. So my question is how to achieve this line:
userQuery?.whereKey(Parse.CHATROOM_USER, matchesKey:Parse.CHATROOM_USER, inQuery: innerRoomQuery)
I know this is wrong because there is no "user" (Parse.CHATROOM_USER) field in the PFUser class. I'm just wondering if there is a way to do this without firing off the roomQuery first and putting the returned PFUsers into an array and then firing the receptionist users query and combining the arrays.
Any ideas?
I'm not entirely sure what you are asking but if you are trying to have a single query find a specific type of user, i would approach this in a different fashion. I would add a bool type to the user class for what I am searching for, and just query for all the users with that bool type as true!

Resources