Getting PFObject from Parse Relation - ios

I have a PFRelation between Users and Offers that the User saved. Each Offer has a pointer reference to a Product PFObject. Even though I am getting the Offers saved for the particular User, the Product PFObject inside the Offer PFObject is not returning completely. Below the code I tried:
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#“offersSaved”];
[[relation query] includeKey:#“offersSaved.product”];
The array returned from fetch contains all the offers I want, but the offers don’t contain the product PFObjects as I would like. Any suggestions?

try this:
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#“offersSaved”];
PFQuery *query = [relation query];
[query includeKey:#“product”];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// ...
}];

Related

IOS Parse is my nested query a good solution for comparing an object with a pointer?

Currently, I am querying the database twice to achieve a list of all users nearby who are not already being followed by the current user. Here is my nested query:
// List of all users being followed by the current user
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kFTActivityClassKey];
[followingActivitiesQuery whereKey:kFTActivityTypeKey equalTo:kFTActivityTypeFollow];
[followingActivitiesQuery whereKey:kFTActivityFromUserKey equalTo:[PFUser currentUser]];
[followingActivitiesQuery setCachePolicy:kPFCachePolicyNetworkOnly];
[followingActivitiesQuery includeKey:kFTActivityToUserKey];
[followingActivitiesQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *followedUserIds = [[NSMutableArray alloc] init];
// Obtain an array of object ids for all users being followed
for (PFObject *object in objects) {
PFUser *followedUser = [object objectForKey:kFTActivityToUserKey];
[followedUserIds addObject:followedUser.objectId];
}
PFGeoPoint *geoPoint = [[PFUser currentUser] objectForKey:kFTUserLocationKey];
// List of all users within 50 miles that are not already being followed
PFQuery *followUsersByLocationQuery = [PFQuery queryWithClassName:kFTUserClassKey];
[followUsersByLocationQuery whereKey:kFTUserObjectIdKey notEqualTo:[PFUser currentUser].objectId];
[followUsersByLocationQuery whereKey:kFTUserLocationKey nearGeoPoint:geoPoint withinMiles:50];
[followUsersByLocationQuery whereKeyExists:kFTUserLocationKey];
[followUsersByLocationQuery whereKey:kFTUserObjectIdKey notContainedIn:followedUserIds];
[followUsersByLocationQuery setLimit:100];
[followUsersByLocationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.objects = objects;
[self.tableView reloadData];
}
}];
}
}];
My question is, is this a viable solution? I feel guilty about having to query the server twice in order to achieve this, but I was not able to do it all in one query. I could not compare a parse pointer from the ActivityClass to the Users class, since the Users class is the class being pointed to, and because of this I couldn't think of a way to do it all in one query.
You're in luck - this can be done in a single query using the whereKey:doesNotMatchKey:inQuery: method of PFQuery.
Parse will treat this as a single, compound query against the database. Totally guilt-free :)
Try this instead:
// List of all users being followed by the current user
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kFTActivityClassKey];
[followingActivitiesQuery whereKey:kFTActivityTypeKey equalTo:kFTActivityTypeFollow];
[followingActivitiesQuery whereKey:kFTActivityFromUserKey equalTo:[PFUser currentUser]];
[followingActivitiesQuery setCachePolicy:kPFCachePolicyNetworkOnly];
[followingActivitiesQuery includeKey:kFTActivityToUserKey];
PFGeoPoint *geoPoint = [[PFUser currentUser] objectForKey:kFTUserLocationKey];
// List of all users within 50 miles that are not already being followed
PFQuery *followUsersByLocationQuery = [PFQuery queryWithClassName:kFTUserClassKey];
[followUsersByLocationQuery whereKey:kFTUserObjectIdKey notEqualTo:[PFUser currentUser].objectId];
[followUsersByLocationQuery whereKey:kFTUserLocationKey nearGeoPoint:geoPoint withinMiles:50];
[followUsersByLocationQuery whereKeyExists:kFTUserLocationKey];
//The next line is your new compound query
[followUsersByLocationQuery whereKey:kFTUserObjectIdKey doesNotMatchKey:#"objectId" inQuery:followingActivitiesQuery];
[followUsersByLocationQuery setLimit:100];
[followUsersByLocationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.objects = objects;
[self.tableView reloadData];
}
}];

Retrieve all guildmembers in the same guild as you using Parse

how do i retrieve all the guildmembers in your guild using parse?
Here is my code:
PFUser *currentuser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"connectedGuild" equalTo:currentuser[#"connectedGuild"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
NSLog(#"There are %d guildmembers. Error:%#", comments.count, error);
}];
My log:
There are 0 guild members. Error:(null)
connectedGuild is a pointer to a guild class where i store all the guilds.
Queries of the PFUser class must be instantiated a little differently. Try this:
PFUser *currentuser = [PFUser currentUser];
PFQuery *query = [PFUser query];
[query whereKey:#"connectedGuild" equalTo:currentuser[#"connectedGuild"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
NSLog(#"There are %d guildmembers. Error:%#", comments.count, error);
}];
For more information, see the Parse.com website here:
https://www.parse.com/questions/get-pfuser-in-pfquery-using-ios-api

How to call data from parse in IOS?

I am trying to retrieve data from parse but I keep getting a not found on object of type PFObject for startingBalance.
Here is my code:
PFQuery *query = [PFQuery queryWithClassName:#"Account"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object.startingBalance);
}
}];
A PFObject is effectively an NSDictionary - at least when it comes to accessing the attributes - so you can access an attribute via objectForKey or the shorthand [] syntax -
PFQuery *query = [PFQuery queryWithClassName:#"Account"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object[#"startingBalance"]);
}
}];
You need to make the object a subclass of your given object type. So in this case, instead of getting an array of generic PFObject you get an array of Account objects.

parse.com objective-c get PfUser object and related object

I am trying to get an object related to my user object. I've tried numerous methods...
this one gets the user object but I don't get the reputation object.
PFuserProfile = [PFQuery getUserObjectWithId:userObj];
PFuserRep = PFuserProfile[#"reputation"];
this one I have an error on the last line.
PFQuery *query = [PFUser query];
[query whereKey:#"objectId" equalTo:userObj];
[testQuery includeKey:#"reputation"];
NSArray *wtf = [query findObjects];
PFuserProfile = [wtf indexOfObject:0];
I've tried some other methods but not sure the best one, I can't get any of them to work... here's the last one where I get an error in setting the profile.
[query whereKey:#"objectId" equalTo:userObj];
[query includeKey:#"reputation"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
PFuserProfile = [objects indexOfObject:objects.firstObject];
}
}];
Thank you.
Make sure that when you select an object from a dictionary use: objectAtIndex:
Therefore you will need to update your code to the following
PFQuery *query = [PFUser query];
[query whereKey:#"objectId" equalTo:userObj];
[testQuery includeKey:#"reputation"];
NSArray *wtf = [query findObjects];
PFuserProfile = [wtf objectAtIndex:0];
Better yet, because you can get an out of bounds exception with objectAtIndex: you can replace the last line with:
PFuserProfile = [wtf firstObject];
This will not crash your application if the query returned no objects.

Trying to query a PFRelation for a specific user in Parse

I'm trying to query a PFRelation of the current user of the app to load a table view showing all of the users friends. Each user is saved under a class called "User" when they first use the app and once they add a friend that friend is added to the column "friendsRelation" for the user. I'm using this code i got got from this link and nothing is returned from the query.
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"friendsRelation" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"An error occurred fetching friends");
}
else {
[self.friends addObjectsFromArray:objects];
[self.tableView reloadData];
}
}];
Try this:
PFRelation *relation = [[PFUser currentUser] relationForKey:#"friendsRelation"];
PFQuery *query = [relation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains all friends of current user
}];
You are not query the PFUser table, so you cannot use [query whereKey:#"friendsRelation" equalTo:[PFUser currentUser]]; to query the User table, since the friendsRelation does not contain any PFUser objects but User objects. Just get the User object which represent for current user and use it to query the relation.
To Query the Users table don't use
[PFQuery queryWithClassName:#"User"];
Use instead
[PFUser query];
Only use the first if you have created your own "User" table and at this point if you checked the Data-browser you will see two different tables both called "User".

Resources