Parse.com Custom cell boolean - ios

I am stuck for a long time with boolean show/hide image. Image is placed in my cell. Only boolean is needed. Could anyone tell me what I am doing wrong?
PFQuery *query = [PFQuery queryWithClassName:#"Parseclass"];
[query whereKey:#"imagebool" equalTo:[NSNumber numberWithBool:[NSNumber numberWithBool:YES]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
cell.discounts.hidden =YES;
}else{
cell.discounts.hidden =NO;
}
}];
EDIT:
PFQuery *query = [PFQuery queryWithClassName:#"parsecell"];
[query whereKey:#"imagebool" equalTo:[NSNumber numberWithBool:YES]];
[query findObjectsInBackgroundWithBlock:^(NSArray *object, NSError *error)
{
if (!error)
{
NSLog(#"Successfully retrieved: %#", object);
NSDictionary *dict = [object objectAtIndex:0];
BOOL boolean;
boolean = [[dict objectForKey:#"imagebool"] boolValue];
if(boolean==YES)
{
NSLog(#"BOOL1: %hhd", boolean);
cell.discounts.hidden = YES;
} else {
cell.discounts.hidden = NO;
NSLog(#"BOOL2: %hhd", boolean);
}
}
else
{
NSLog(#"Error: %#", [error localizedDescription]);
}
}];

Change below stuff :-
[NSNumber numberWithBool:[NSNumber numberWithBool:YES]] //No need for this double encoding.
Simple go with [NSNumber numberWithBool: YES].
Also
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error && objects) //Checking for object's just to know if your getting any result for condition that you provided in query.
{
//Then hide your image.
}else{
//Or show your image.
}
}];
Just in case if your storing image on parse and check if image is nil or stored. Then just simple call for column that is storing image and no boolean check condition. That way you would get object's and know which row/column in your app is to show/hide image. Also in that object you will get image and provide it in your app and no need to store on local memory of phone. Anything else do let me know.
UPDATE:-
As you will get the object(array).
[query findObjectsInBackgroundWithBlock:^(NSArray *object, NSError *error)
{
if (!error)
{
NSLog(#"Successfully retrieved: %#", object);
NSDictionary *dict = [object objectAtIndex:0];
//Now with dict you could simply check for key(boolean) and then depending on YES/NO ,you could hide/Show the image.
}
else
{
NSLog(#"Error: %#", [error localizedDescription]);
}
}];
Hope this might help you.

Two problems I see with your code:
For some reason you're double encoding YES, i.e. [NSNumber numberWithBool:[NSNumber numberWithBool:YES]], probably won't break things, but fix it
In the block you're checking for an error, but you're not checking the objects returned. The array will come back empty if nothing matched the query, add a check for objects.count > 0

Related

Simple Parse.com with findObjectsInBackgroundWithBlock does not return

I am trying to query the _User Class from Parse to use it on a TableViewCell. I am getting a nil value from it. I did have it working at one time with a much simpler, but not recommended, query:
Working query (not recommended):
- (NSArray *)loadUsers {
PFQuery *users = [PFUser query];
return [users findObjects];}
Not working query:
- (void)loadAllUser:(void (^)(BOOL completion))completion {
PFQuery *query = [PFUser query];
__block NSArray *loadUsers = [NSArray new];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (User *user in objects) {
loadUsers = [loadUsers arrayByAddingObject:user];
}
[TaskController sharedInstance].loadAllUser = loadUsers;
}else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
I am subclassing the _User Class from Parse (just FYI)
The problem was when the method was called. The numberOfRowsInSection was not was passing a nil value.

How to Fetch all data in a table using PFQuery in iOS?

i am newbie here in iOS and Parse Framework i want to fetch data from my Parse table from PFQuery like as
NSUInteger limit = 1500;
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query setLimit: limit];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error) {
NSLog(#"Successfully retrieved: %#", objects);
} else {
NSString *errorString = [[error userInfo] objectForKey:#"error"];
NSLog(#"Error: %#", errorString);
}
}];
it is Working as i want but it is give me only 1000 objects i want here to fetch all my table data it contain unto 2000 object and it will increment as day by day please help me for this.
For this Now i write a code like this but it is Only Give me 1000 Objects only
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 0;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[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) {
NSLog(#"Array %#",objects);
}];
}
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
thanks.
I believe there is a query limit on the number of objects that can be fetched. What I would do is query make two queries for the same thing but for the second query, do something like this
[query setSkip1000];
you can skip the first 1000 objects in the first query and grab the next 1000. Make your array an NSMutableArray and inside each block do this
[self.myArray addObjects:objects]; instead of self.myArray = objects; that way you are overwriting the objects in your array.
Edit
Instead of 2 separate queries you can also do this
NSMutableArray *allObjectsArray = [NSMutableArray array];
//Set this to the amount of objects you want. Has to be be 1000 or less
NSUInteger limit = 0;
//Set this to the amount you want to skip (Usually 0)
NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:#"tempClass"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. Add the returned objects to allObjects
[allObjectsArray addObjectsFromArray:objects];
if (objects.count == limit) {
// There could be more objects in your database. Update the skip number and perform the same query.
skip = skip + limit;
[query setSkip: skip];
[query findObject...// Exactly the same way as you did before to get the rest of your objects in the database
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
This is a simple recursive solution to retrieve all the objects from a class using blocks.
Here is how you initially call it.
[self queryAllObjectswithLimit:1000 withSkip:0 withObjects:#[] withSuccess:^(NSArray * objects) {
//All the objects
} failure:^(NSError * error) {
//
}];
Here is the method.
- (void)queryAllObjectswithLimit:(NSUInteger )limit withSkip:(NSUInteger )skip withObjects:(NSArray *)objects withSuccess:(void (^)(NSArray *objects))success failure:(void (^)(NSError *error))failure {
//Store all the Objects through each layer of recurrsion
NSMutableArray *allObjects = [NSMutableArray arrayWithArray:objects];
PFQuery *query = [PFQuery queryWithClassName:#"Class_Name"];
query.limit = limit;
query.skip = skip;
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
if (!error) {
// The find succeeded. Add the returned objects to allObjects
[allObjects addObjectsFromArray:objects];
if (objects.count == limit) {
//Recursively Call this until count does not equal limit, then begin returning all the objects back up
[self queryAllObjectswithLimit:limit withSkip:skip+limit withObjects:allObjects withSuccess:^(NSArray * objects) {
//This will return everything
success(objects);
} failure:^(NSError * error) {
failure(error);
}];
} else {
success(allObjects);
}
} else {
failure(error);
}
}];
}
I've tested this with less than 1000 objects, 1000 objects, and more than 1000 objects and it works perfectly.
Be cautious of how many objects you're grabbing though because this will grab all of them and if you're working with a large data set that might be an issue in terms of memory very quickly.
https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery
You can use the skip and limit parameter to paginate through all objects in the table by adding the value of limit to skip until the query returns an amount of objects that is less than limit.
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 0;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. Add the returned objects to allObjects
[allObjects addObjectsFromArray:objects];
if (objects.count == limit) {
// There might be more objects in the table. Update the skip value and execute the query again.
skip += limit;
[query setSkip: skip];
[query findObjects... // Execute the query until all objects have been returned. Keep adding the results to the allObjects mutable array.
}
} else {
// Log details of the failure
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]);
}
}];

PFQuery on PFUser returns nil

I'm new to Parse and I'm trying to retrieve a list of friends for my current user, but my result is always nil.
Here's how my data is on the server:
Class User (auto-created when first user signed up using the PFUser class)
Inside the class User I have a field called "array_friends_id" which is an array and it contains multiple objectIds inside. This field already has an array in it with 1 string inside
Here's my code for iOS:
- (IBAction)button_login:(id)sender
{
[PFUser logInWithUsernameInBackground:[textField_username text]
password:[textField_password text]
block:^(PFUser *user, NSError *error)
{
if (!error)
{
NSLog(#"logged in!");
// find friends
PFQuery *query = [PFQuery queryWithClassName:#"Users"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
NSLog(#"objects = %#", objects);
}
}];
}
else
{
NSLog(#"can't login. Error = %# %#", error, [error userInfo]);
}
}];
}
My idea is to allow the user to log into the app, and as soon as the user logs in I want to retrieve his list of friends, even before pushing to the next view controller.
What am I doing wrong?
This is how I query in Parse,
PFQuery *query = [PFUser query];
[query orderByAscending:#"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSString *errorString = [error userInfo][#"error"];
NSLog(#"Log %#", errorString);
}
else {
// all good
}
}];

Displaying Username - Parse

I have looked ALL over the internet and found nothing, Please help me. Im trying to figure out how I can display a username from parse in my table view (detailTextLabel). When I do the code below my app only shows the PFUser Id not the username. It will display: <PFUser:H2AhEbYGal:(null)> { but I'm trying to display the username of that post.
Heres me saving the object:
PFUser *user = [PFUser currentUser];
PFObject *quoteNew = [PFObject objectWithClassName:#"New"];
quoteNew[#"author"] = user;
[quoteNew setObject:[[self quoteText] text] forKey:#"quoteText"];
Here is me trying to retrieve the user in a PFQuery:
PFQuery *query = [PFQuery queryWithClassName:#"New"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. The first 100 objects are available in objects'
[query includeKey:#"author"];
// PFObject *testObject = [query findObjects];
// PFUser *testUser = [testObject objectForKey:#"author"];
// NSLog(#"username: %#",testUser.username);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Here is me trying to display it:
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#",[object objectForKey:#"author"],[dateFormat stringFromDate:updated]];
You can just worry about [object objectForKey:#"author"] in that cell.detailTextLabel.text code.
This:
PFQuery *query = [PFQuery queryWithClassName:#"New"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. The first 100 objects are available in objects'
[query includeKey:#"author"];
// PFObject *testObject = [query findObjects];
// PFUser *testUser = [testObject objectForKey:#"author"];
// NSLog(#"username: %#",testUser.username);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Should be:
PFQuery *query = [PFQuery queryWithClassName:#"New"];
// BEFORE WE QUERY
[query includeKey:#"author"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Get your objects here
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
And this:
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#",[object objectForKey:#"author"],[dateFormat stringFromDate:updated]];
Should be:
PFUser *userToDisplay = object[#"author"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#",userToDisplay.username,[dateFormat stringFromDate:updated]];
Because, as it stands, problem 1 is you're saying to include the author AFTER you have already queried parse. Problem 2 is that you're trying to display the whole user, you need to get the user, and then print the username value.

Resources