How to Fetch all data in a table using PFQuery in iOS? - 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]);
}
}];

Related

Parse: Find number of objects found in query

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

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

Display the username of query results (Parse.com on iOS)

I am currently using the query whereKey:#"name" equalTo:#"Tom Coomer" and it is working.
However I would like to create an array of the usernames from the results.
How would I approach this?
Thanks
I am using Parse.com and Xcode to write the app.
Well, I assume you're saving your query results to an array, so what you want to do is this:
NSMutableArray *array = [NSMutableArray array]
for(PFUser *user in arrayFromQuery)
{
[array addObject:user.username];
}
What this does is traverse through your array and gets the username from each user and adds it to a new array.
If you want to fetch only the username from the Parse :
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query selectKeys:#[#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// objects in results will only contain the username fields
}];
result array contains the name of all user.
And if u want to fetch all records and then create a array of username:
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d user.", objects.count);
// Do something with the found objects
for (PFUser *object in objects)
{
[self.usernameArray addObject:object.username];
NSLog(#"%#", object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
where usernameArray is a NSMutableArray.

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

Resources