Fetch objectId from a User in parse - ios

i'm new to parse and i'm trying to fetch a user objectId, but whatever i cant seem to return the objectId. I can easily return the username. this is my query:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
NSLog(#"%#", [objects lastObject]);
}
}
}];
This returns something like this:
<PFUser:9dcc65tsdr:(null)> {
username = AEleQFdBx9jdtypfsQmLtzAvW;
}
How do i return the objectId which is between PFUser and (null)?

You can use object.objectId like this:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
for (PFObject *object in objects){
NSLog(#"Object ID: %#", object.objectId);
}
}
}

You can also use this: [[PFUser currentUser]objectId] much faster and works much better because you don't have to run a whole PFQuery.

Related

How to fetch Data From Same Key in Parse iOS?

i am newbie in to Parse.com i try to Fetch data from Parse table with same key but value is Different like as
-(void)getdata
{
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 1000;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query whereKey:#"Type" containedIn:#[#"Temopary", #"Business"]];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[allObjects addObjectsFromArray:objects];
if (objects.count == limit) {
skip += limit;
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[allObjects addObjectsFromArray:objects];
self.lqpinname=[allObjects valueForKey:#"GPIN"];
NSLog(#"Qpin name COunt %#",self.lqpinname);
self.locationArray=[allObjects valueForKey:#"Location"];
self.llatitude=[self.locationArray valueForKey:#"lat"];
self.llongitude=[self.locationArray valueForKey:#"lng"];
self.laddress=[allObjects valueForKey:#"Address"];
self.lusernameArray=[allObjects valueForKey:#"AddedBy"];
hudView.hidden=TRUE;
}];
}
}
else
{
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
But it is return Null value here i want to Fetch data from table where column Type=Business & Temporary Please Give me Solution for this.
thanks.
You should be able to use whereKey:containedIn: to do this.
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query whereKey:#"Type" containedIn:#[#"Temopary", #"Business"]];
You also have a typo in Temporary (unless Temporay is something) - not sure if that's intentional or not.

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

Parse Caching Issue IOS

I'm having problem while caching query. Below is the code I am using for my query. If i comment out [query whereKey:#"login" equalTo:currentUser.login] the query is caching fine.
//Defining the query
+(PFQuery *)queryForListingForCurrentUser{
PFQuery *query = [Listings query];
BOOL hasCache = [query hasCachedResult];
NSLog(hasCache ? #"yes": #"no");
User *currentUser = (User *)[PFUser currentUser];
[query whereKey:#"login" equalTo:currentUser.login];
query.cachePolicy = kPFCachePolicyNetworkElseCache;
return query;
}
//Calling the query
PFQuery *query = [Listings queryForListingForCurrentUser];
[query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error){
}];

Retrieving data from parse.com and showing them

I'm trying to retrieve data from parse.com which i have already sent.
I'm using following code to send the data:
-(IBAction)msgSend:(id)sender {
PFObject *appMsg = [PFObject objectWithClassName:#"appMsg"];
appMsg[#"besked"] = msg.text;
[appMsg saveInBackground];
msg.text = #"";
}
And to get the data I'm trying this:
PFQuery *query = [PFQuery queryWithClassName:#"appMsg"];
[query getObjectInBackgroundWithId:#"*******" block:^(PFObject *appMsg, NSError *error) {
NSString *besked = appMsg[#"besked"];
msgRecieved.text = besked;
}];
At last I'm trying to display the data into a textView.
Im not really loading any data?
PFQuery *query = [PFQuery queryWithClassName:#"tempClass"];
[query orderByDescending:#"createdAt"];
query.limit =10;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Retrive values like below,object id is retrieved by objects.objectId
NSLog(#"Object values %#",[objects valueForKey:#"columnName"]);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];

What should I put on the whereKey:#"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);
}
}];

Resources