Catch 'no results found' in Parse PFQuery - objective-c

When using the PFQuery, if no results are found it just outputs it to the console but does not let me handle the error in the code. I have not found this documented on their site so I hope to seek help here. I am using:
findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
to try to find the objects. I have tried seeing if objects was nil, error was nil, and if objects.count was equal to zero but none of the above worked.
Does anyone know how to handle the case of the result not being in the query using a PFQuery? Thank you.
EDIT 10/10/2015: My Code:
PFQuery *query = [PFQuery queryWithClassName:#"Group"];
[query getObjectWithId:s];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error && [objects count] > 0) {
// The find succeeded.
}
}else if ([objects count] == 0) {
// Should run when there is nothing found
}
else {
// Log details of the failure
}
}];
When I run the query and get no results, nothing in the block runs.

Related

Parse: Query to change labels text

I can currently query a parse class, but can't figure out how to change a labels text if the returned values match the query. I am relatively new to objective C and Parse so my knowledge on the subject is little. My query looks like this (with the text of what i'm trying to achieve underneath).
PFQuery *FTQ0 = [PFQuery queryWithClassName:#"Class1"];
[FTQ0 whereKey:#"Location" equalTo:#"The Shop"];
//Label.text = query (object?)
Thanks in advance.
Here is a solution. You have to be careful though, because there might be many objects that have key Location and equal to The Shop. This is why parse is returning an array of objects. In this case, I pick the first object in the array and display it.
PFQuery *query = [PFQuery queryWithClassName:#"Class1"];
[query whereKey:#"Location" equalTo:#"The Shop"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
Label.text = [NSString stringWithFormat:#"%#", [[objects firstObject] objectForKey:#"WHATEVER YOU WANT TO DISPLAY EX. NAME, LOCATION..."]]
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
For more information please visit https://parse.com/docs/ios/guide
Something like this :
PFQuery *query = [PFQuery queryWithClassName:#"Class1"];
[query whereKey:#"Location" equalTo:#"The Shop"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error && object) {
// Do your stuff
Label.text = [[object objectForKey:#"YOUR KEY"] stringValue];
} else {
// Error or null object
}
}];
In this example the query return only the first object.
Thanks so much for your quick and helpful response! This was an issue that was troubling me allot! The code provided works!
Thanks!

Get int value out of pfquery to change settings objective-c

Im trying to work with a script thats in objective c i want to make an int i can access throughout view did load to change settings or the viewcontroller but it doesn't work like swift and i get a null out of the pfquery so i tried to make an array and couldn't get anything out of the query with that as well
what is the best way to set up and int variable and set it in the pfquery then use if statements later on
here is some of my code
__block int setting = 0;
PFQuery *query = [PFQuery queryWithClassName:#"test"];
[query whereKey:#"testActive" equalTo:[NSNumber numberWithBool:YES]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %lu scores.", (unsigned long)objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(#"%#", object.objectId);
setting = [[object objectForKey:#"testSetting"] intValue];
// When checked in the loop i get all the right data
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
// When checked here i get settings = 0 when it should be 1
if (setting == 0) {
self.segmentedControl.selectedSegmentIndex = 0;
} else if (setting == 1) {
self.segmentedControl.selectedSegmentIndex = 1;
}
if (setting == 3) {
[self.segmentedControl setEnabled:YES];
}else {
[self.segmentedControl setEnabled:NO];
}
But when i check the setting int its always 0 but if i log in the loop is gives me the correct number
thanks for your help
Thanks for your help but i figured it out..
findObjectsInBackgroundWithBlock was not completing before i did the if statements
so i moved the code into a new function which i ran when the PFQuery was completed giving me the correct int

PFQuery Array Issue

I have the following code which is loading a list of users from Parse into an array named users:
PFUser *currentUser = [PFUser currentUser];
NSString *currentUserUni = currentUser[#"university"];
//Parse Query
//-------------------------------------------
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"university" equalTo:currentUserUni];
[query setLimit:50];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[users removeAllObjects];
[users addObjectsFromArray:objects];
}];
But if I try to call users outside of that closing bracket at the end, it is nil. Does anyone know
1)what would be causing this?
and
2)How can I get the values into an array that can be accessed outside of that closing bracket?
1) findObjectsInBackgroundWithBlock retrieves objects in the background, meaning, it happens asynchronously - the method runs while other processes are running instead of sequentially. So say you have your current code:
Note I would, supply an error check before messing with your users array.
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"university" equalTo:currentUserUni];
[query setLimit:50];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[users removeAllObjects];
[users addObjectsFromArray:objects];
} else {
// show your alert message
}
}];
... some code that comes after the block ...
The "... some code that comes after the block ..." portion of your 'class' or 'method' will be running while your searching for the objects in the background...
2) What needs to be done to solve your issue is add the __block keyword to your users array.
Heres a reference to an answer in stack overflow that further explains the __block keyword.
__block keyword
takeing jsetting32 answer and changing it
i think you should take removeAllObjects out of the loop
[users removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"university" equalTo:currentUserUni];
[query setLimit:50];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[users addObjectsFromArray:objects];
} else {
// show your alert message
}
}];
... some code that comes after the block ...

How to delete image from Parse?

I want to delete / overwrite a photo to Parse for a given user. Will you help me? Because I wrote the code that I always return error 102
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"User" equalTo:[PFUser currentUser]];
[query includeKey:#"Foto"];
[query whereKeyExists:#"Dev.png"]; //Change this condition to fetch the row.
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object1, NSError *error)
{
if (!error) {
NSLog(#"Successfully retrieved: %#", object1);
//This might work as I searched for this deleting image but there is no method to do so.
//So a way would be is to get that object and then setting nil value in it's place.
//Else if u want to delete the entire row then u could simply type, [object deleteInBackground]
object1[#"Foto"] = [NSNull null];
}
else
{
NSLog(#"Error: %#", [error localizedDescription]);
}
}];
This is my table:
Change this line:
[query whereKey:#"User" equalTo:[PFUser currentUser]];
With below line:
[query whereKey:#"username" equalTo:[PFUser currentUser]];

Setting block value in Objective-C using Parse [duplicate]

This question already has answers here:
findObjectsInBackgroundWithBlock: gets data from Parse, but data only exists inside the block
(2 answers)
Closed 9 years ago.
I'm using Parse.com in my objective-c code to simply check to see if a record is there and return true if it is and false if it's not, here's my code:
- (BOOL)userExists:(NSString*) email{
__block BOOL exists = NO;
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"email" equalTo:email];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d objects.", objects.count);
// Do something with the found objects
if (objects.count > 0) {
exists = YES;
NSLog(#"Objects > 0");
}else{
exists = NO;
NSLog(#"Objects = 0");
}
} else {
// Log details of the failure
//NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
return exists;
}
This method for some reason is returning NO, even though there is a row that contains that email address I'm looking for. Any advice would be greatly appreciated.
Your block is working in the background, thus, your method is returning the value NO before your block completes the work.