Queries on array values on Parse.com isn't working - ios

This is basically killing my app and Parse isn't exactly being prompt with their responses.
Queries on pointer arrays aint working.
Try this in your app:
- (void)runTest{
// Run a build to store 20 rows.
for(int i=0;i<20;i++){
NSMutableArray *recipients = [NSMutableArray array];
[recipients addObject:[PFUser currentUser]];
PFObject *message = [PFObject objectWithClassName:#"Run"];
[message addObjectsFromArray:recipients forKey:#"recipients"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
}
}
- (void)runTestQuery{
// Run a second build to query the 20 rows. Not all 20 are returned.
PFQuery *query = [PFQuery queryWithClassName:#"Run"];
[query whereKey:#"recipients" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(#"Objects count: %li",(long)objects.count);
}];
}

Related

Parse query finds two objects when there is only one

I have the following code to find the objects in a custom class. For some reason it is finding two of the same object (self.addedArray2's count is 2, but on Parse data table online, there is only one). Do you see why it is finding the same object twice?
self.array2 = [[NSMutableArray alloc] init];
self.addedArray2 = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:#"Friends"];
[query whereKey:#"user" containsString:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (Friends *currentFriend in objects) {
self.relation = currentFriend.friendsRelation;
self.addedRelation = currentFriend.addedRelation;
self.query = [_relation query];
[[_relation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self.array2 addObjectsFromArray:objects];
[[_addedRelation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self.addedArray2 addObjectsFromArray:objects];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Friends (%lu)", (unsigned long)[self.array2 count]] forSegmentAtIndex:0];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Added Me (%lu)", (unsigned long)[self.addedArray2 count]] forSegmentAtIndex:1];
NSLog(#"Number 1: %#", [self.addedArray2 objectAtIndex:0]);
NSLog(#"Number 2: %#", [self.addedArray2 objectAtIndex:1]);
}];
}];
}
}];
It is logging "Number 1" then "Number 1" then "Number 2". It looks like that code is being run twice and adding it again. How do I fix that? With all this nesting, I don't really see what I can do.
For your key "user" of the Friends class, if it String that holds the user's username, try replacing:
[query whereKey:#"user" containsString:[[PFUser currentUser] objectForKey:#"username"]];
with this and let me know if that changes anything:
[query whereKey:#"user" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
If it is a pointer to a user object, however, then you will want your query to resemble something along these lines:
[query whereKey:#"user" equalTo:[User currentUser]];
If either of my assumptions are wrong, let me know because I do not know the structure of your classes, but I will try and figure it out.

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.

Parse relationships and searches specific pfQuery PFObject

I want to use as the backend of my app Parse, and suppose that I have a class with different restaurants, menus for each restaurant and for each menu different products, I have a class Place, Menu, Product and MenuItems:
The MenuItem class has:
Pointe menu
Pointer product
The Menu class:
Pointer place
Once chosen restaurant have to show all products for that restaurant:
My Code:
PFQuery *query = [PFQuery queryWithClassName:#"Places"];
[query whereKey:#"name" equalTo:PlaceSelect];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
PFObject *Menu;
if (!error)
{
for (PFObject *ob in objects)
{
Menu = ob;
}
}
PFQuery *query1 = [PFQuery queryWithClassName:#"Menus"];
[query1 whereKey:#"place" equalTo:Menu];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects2, NSError *error2) {
PFObject *MenuItems;
if (!error2)
{
for (PFObject *ob2 in objects2)
{
MenuItems = ob2;
}
}
PFQuery *query2 = [PFQuery queryWithClassName:#"MenuItems"];
[query2 whereKey:#"menu" equalTo:MenuItems];
[query2 selectKeys:#[#"product"]];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects3, NSError *error3) {
PFObject *Products;
if (!error3)
{
for (PFObject *ob3 in objects3)
{
Products = ob3;
NSLog(#" %# ",Products);
}
I get related products but when I want to filter by type: drinks, starters, etc... It gives me all the products of all places them...
PFQuery *retrievedDrink = [PFQuery queryWithClassName:#"Products"];
[retrievedDrink whereKey:#"type" equalTo:#"drink"];
[retrievedDrink whereKey:#"objectId" equalTo:Products.objectId];
[retrievedDrink findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
DrinksArray = [[NSArray alloc] initWithArray:objects];
NSLog(#"numero de productos Drinks= %i ",[DrinksArray count]);
}];
Thanks in advance, if anyone knows a way to more efficiently please I would like to clarify my doubts
Get rid of this line
[retrievedDrink whereKey:#"objectId" equalTo:Products.objectId];
That is returning all your objects.
You have a duplicate array of your objects that is not needed, and also, do an error check.
This should find all the drinks stored in your Parse class, store them in an array called objects and allow you to query that array in your findObjectsInBackgroundWithBlock
PFQuery *retrievedDrink = [PFQuery queryWithClassName:#"Products"];
[retrievedDrink whereKey:#"type" equalTo:#"drink"];
[retrievedDrink findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"numero de productos Drinks= %i ",[objects count]);
}
}];

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.

Resources