In my "Message" table in Parse I have a field called conversation, which is a pointer to a Conversation (another table in my database).
To query for a Message, can I do:
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"conversation" equalTo:_conversation.objectid];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
or do I have to get the actual PFObject *myConversation and use that...
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
It seems that #1 doesn't work, but #2 does...my question is how can I get #1 to work (i.e. use a PFObject's id to query when I have a pointer field)
.objectId is just a string, if your "conversation" key contains a pointer to myConversation, then you must include a PFObject in the equal to.
If you only have the objectId, you can search pointers without data using:
PFObject * myConversation = [PFObject objectWithoutDataWithClassName:#"Conversation" objectId:_conversation.objectid];
// continue here
[messageQuery whereKey:#"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
Related
I am trying to print the name of a category using Parse in iOS and Objectve-C. Thanks for looking. Let me know if you have any questions.
CategoryInBudget Table
category(pointer)
amount(number)
user(pointer)
Category Table
name(string)
Here is my code:
PFQuery *query = [PFQuery queryWithClassName:#"CategoryInBudget"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
[query includeKey:#"Category"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object[#"Category"][#"name"];
}
}];
I have a Filter class which has a pointer to a user in my User class. I'm wondering how can I get the Filter object in the Filter class which is equal to the [PFUser CurrentUser] and then get both column values from the User and Filter class
PFQuery *filterQuery = [PFQuery queryWithClassName:#"Filter"];
filterQuery.limit = 1;
[filterQuery whereKey:#"userId" equalTo:[PFUser currentUser].objectId];
[filterQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
NSLog(#"%#", objects);
}
}];
You just use the object itself...
PFQuery *filterQuery = [PFQuery queryWithClassName:#"Filter"];
filterQuery.limit = 1;
[filterQuery whereKey:#"userId" equalTo:[PFUser currentUser]]; //no need to put objected here.
[filterQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
NSLog(#"%#", objects);
}
}];
As long as the userId field is a pointer to the _User object then this will work.
If userId is a pointer to _User then you can add...
[filterQuery includeKey:#"userId"];
This will then populate the userId object of the Filter objects with data when they are sent down. If you don't put that line then you will just get the objectId in the userId object.
However, I'm not sure you've done it as a Pointer. Can you confirm that it definitely is.
You should have this in the Data Browser table for Filter...
(follower will say userId on yours)
I just try to write some sample app using parse backend, i created app and test class, its worked fine for ios, for same calls if i make a PFquery call to retrive objects its crashing on mac.
PFQuery *query = [PFQuery queryWithClassName:#"stock"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (query.cachePolicy != kPFCachePolicyCacheOnly && error.code == kPFErrorCacheMiss) {
// No-op on cache miss - since the policy is not CacheOnly, this
// block will be called again upon receiving results from the network.
return;
}}];
You are setting cachePolicy wrong.
Use this:
PFQuery *query = [PFQuery queryWithClassName:#"stock"];
[query setCachePolicy:kPFCachePolicyNetworkOnly];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
}
}];
In parse i have a "Game" class with array pointer column called "Players". I want to get games that contain [PFUser currentUser] in arrayKey called "Players", always this column stored a three players array.
I tried some query, i couldn't get success.
PFQuery *query = [PFQuery queryWithClassName:#"Game"];
[query whereKey:#"Players" containsAllObjectsInArray:#[[PFUser currentUser]]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{...}];
Also tried.
[query whereKey:#"Players" containsAllObjectsInArray:#[[PFUser currentUser].objectId]];
I'm sure already stored a Game with current User inside.
Thanks in advanced.
Just do this:
PFQuery *query = [PFQuery queryWithClassName:#"Game"];
[query whereKey:#"Players" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{...}];
If you run equalTo on an array key, it checks to see if the value is in the array.
Make sure to check that you stored a pointer to the user, and not their objectId. If you're storing id's instead, do equalTo:[PFUser currentUser].objectId];
I'm using iOS.
If I insert #"objectId" in the whereKey:#"". I get an error saying: bad special key: objectId.
This is my code:
PFQuery *findFriends = [PFUser query];
[findFriends whereKey:#"objectId" equalTo:friendsID];
[findFriends selectKeys:#[#"firstname",#"lastname"]];
[findFriends findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"%#", objects);
}
}];
friendsID is an NSString with the objectId from the user the current user is following.
Thank you!
The problem is that your friendsID is not actually a string, my guess is that it's a PFUser. If this is the case, then one way to do it would be to user this.
PFUser * toUser = [friends[0] objectForKey:#"toUser"];
PFQuery * findFriends = [PFUser query];
[findFriends whereKey:#"objectId" equalTo:toUser.objectId];
[findFriends selectKeys:#[#"firstname",#"lastname"]];
[findFriends findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"%#", objects);
}
}];