Parse: Find number of objects found in query - ios

How do I find the number of objects found in a query? The following code is always printing "0", but there is a user with that username in the database.
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"username" equalTo:self.usernameField.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if ([objects count] == 0) {
NSLog(#"error %lu", (unsigned long)[objects count]);
}
else {
NSLog(#"no error");
}
}];
What am I doing wrong?

Performing a query on the _User class should be done using the +query method of the PFUser class
PFQuery *userQuery = [PFUser query]; //Note the difference here.
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if ([objects count] == 0) {
NSLog(#"error %lu", (unsigned long)[objects count]);
}
else {
NSLog(#"no error");
}
}];

Try This , Because parse default user entity is initiated with "_User"
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"username" equalTo:self.usernameField.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if ([objects count] == 0) {
NSLog(#"error %lu", (unsigned long)[objects count]);
}
else {
NSLog(#"no error");
}
}];

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.

How to fetch Data From Same Key in Different array Parse iOS? [duplicate]

This question already has an answer here:
How to fetch Data From Same Key in Parse iOS?
(1 answer)
Closed 7 years ago.
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"];
[query whereKey:#"Type" containedIn:#[#"Temporary", #"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.qpinname=[allObjects valueForKey:#"GPIN"];
NSLog(#"Qpin Array %lu",(unsigned long)[self.qpinname count]);
self.locationArray=[allObjects valueForKey:#"Location"];
self.latitude=[self.locationArray valueForKey:#"lat"];
self.longitude=[self.locationArray valueForKey:#"lng"];
self.address=[allObjects valueForKey:#"Address"];
self.usernameArray=[allObjects valueForKey:#"AddedBy"];
[self getdata2];
hudView.hidden=TRUE;
}];
}
}
else
{
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
-(void)getdata2
{
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 1000;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query whereKey:#"Type" equalTo:#"Personal"];
[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 Array %lu",(unsigned long)[self.lqpinname count]);
self.llocationArray=[allObjects valueForKey:#"Location"];
self.llatitude=[self.llocationArray valueForKey:#"lat"];
self.llongitude=[self.llocationArray valueForKey:#"lng"];
self.laddress=[allObjects valueForKey:#"Address"];
self.usernameArray=[allObjects valueForKey:#"AddedBy"];
}];
}
}
else
{
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
Here i want to fetch data for same key but in different array but it is not working for me please give me solution for it.
thanks.
i am not sure if this is what you want to do but:
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query whereKey:#"Type" containedIn:#[#"Temporary", #"Business", #"Personal"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *result, NSError *error) {
if (!error && result.count)
{
for (PFObject *parseResponse in result)
{
if ([[parseResponse objectForKey:#"Type"] isEqual:#"Personal"])
{
//the logic for data with Type = Personal
}
else
{
//the logic for the data with other Types
}
}
}
else
{
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];

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]);
}
}];

Fetch objectId from a User in parse

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.

Parse friend request handling

I am doing a query in parse to check friend request. The query returns objects and I could log them. Now when I convert it to PFUSER, and NSLog, it gets crashed with error: 'Key "FirstName" has no data. Call fetchIfNeeded before getting its value.' Please help me out here.
PFQuery * friendQuery = [PFQuery queryWithClassName:#"friendship"];
[friendQuery whereKey:#"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:#"status" equalTo:#"Pending"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
//NSLog error
}
else {
NSLog(#"friendRequestCount = %d", objects.count);
PFObject *object=[objects objectAtIndex:0];
NSLog(#"%#", object);
self.friendRequestArray= objects;
NSLog(#"%#",object[#"toUser"]);
PFUser *user1= object[#"toUser"];
NSString *friendName = [NSString stringWithFormat:#"%# %#",user1[#"FirstName"], user1[#"LastName"] ];
NSLog(#"name= %#",friendName);
}
}];
You can use 1 or 2, I have comment the below code, not test.
PFQuery * friendQuery = [PFQuery queryWithClassName:#"friendship"];
[friendQuery whereKey:#"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:#"status" equalTo:#"Pending"];
//1
[friendQuery includeKey:#"toUser"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
//NSLog error
}
else {
NSLog(#"friendRequestCount = %d", objects.count);
PFObject *object=[objects objectAtIndex:0];
NSLog(#"%#", object);
self.friendRequestArray= objects;
NSLog(#"%#",object[#"toUser"]);
PFUser *user1= object[#"toUser"];
//2 Change PFUser to PFObject
PFObject *user1 = object[#"toUser"];
[user1 fetchIfNeeded];
NSString *friendName = [NSString stringWithFormat:#"%# %#",user1[#"FirstName"], user1[#"LastName"] ];
NSLog(#"name= %#",friendName);
}
}];

Resources