How to call data from parse in IOS? - ios

I am trying to retrieve data from parse but I keep getting a not found on object of type PFObject for startingBalance.
Here is my code:
PFQuery *query = [PFQuery queryWithClassName:#"Account"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object.startingBalance);
}
}];

A PFObject is effectively an NSDictionary - at least when it comes to accessing the attributes - so you can access an attribute via objectForKey or the shorthand [] syntax -
PFQuery *query = [PFQuery queryWithClassName:#"Account"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object[#"startingBalance"]);
}
}];

You need to make the object a subclass of your given object type. So in this case, instead of getting an array of generic PFObject you get an array of Account objects.

Related

parse - edit array in parse user class

I have a little problem with loading and sending data to parse.com.
I want to read an Array from parse User Class, adding it and send it back to parse.com.
The following code shows how it would work for a "normal" Class to send an array:
PFQuery *query = [PFQuery queryWithClassName:#"myOwnClass"];
[query getObjectInBackgroundWithId:#"myObjectID"
block:^(PFObject *gameScore, NSError *error) {
[gameScore addUniqueObjectsFromArray:#[#"Name1", #"Name1"] forKey:#"Friends"];
[gameScore saveInBackground]; }];
To Get The Array...Change your PFQuery Method:
-(void)FetchFromPFUserClass{
PFQuery*query = [PFUser query];
[query getObjectInBackgroundWithId:#"myObjectID"
block:^(PFObject *gameScore, NSError *error) {
if(error==nil){
[gameScore addUniqueObjectsFromArray:#[#"Name1", #"Name1"] forKey:#"Friends"];
[gameScore saveInBackground];
}
}];
}

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

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.

Parse query retrieve pointer columns

I have a Filter class which has a pointer to a user in my User class. I'm wondering how can I get the Filter object in the Filter class which is equal to the [PFUser CurrentUser] and then get both column values from the User and Filter class
PFQuery *filterQuery = [PFQuery queryWithClassName:#"Filter"];
filterQuery.limit = 1;
[filterQuery whereKey:#"userId" equalTo:[PFUser currentUser].objectId];
[filterQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
NSLog(#"%#", objects);
}
}];
You just use the object itself...
PFQuery *filterQuery = [PFQuery queryWithClassName:#"Filter"];
filterQuery.limit = 1;
[filterQuery whereKey:#"userId" equalTo:[PFUser currentUser]]; //no need to put objected here.
[filterQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
NSLog(#"%#", objects);
}
}];
As long as the userId field is a pointer to the _User object then this will work.
If userId is a pointer to _User then you can add...
[filterQuery includeKey:#"userId"];
This will then populate the userId object of the Filter objects with data when they are sent down. If you don't put that line then you will just get the objectId in the userId object.
However, I'm not sure you've done it as a Pointer. Can you confirm that it definitely is.
You should have this in the Data Browser table for Filter...
(follower will say userId on yours)

Parse ios wherekey using objectid

In my "Message" table in Parse I have a field called conversation, which is a pointer to a Conversation (another table in my database).
To query for a Message, can I do:
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"conversation" equalTo:_conversation.objectid];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
or do I have to get the actual PFObject *myConversation and use that...
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
It seems that #1 doesn't work, but #2 does...my question is how can I get #1 to work (i.e. use a PFObject's id to query when I have a pointer field)
.objectId is just a string, if your "conversation" key contains a pointer to myConversation, then you must include a PFObject in the equal to.
If you only have the objectId, you can search pointers without data using:
PFObject * myConversation = [PFObject objectWithoutDataWithClassName:#"Conversation" objectId:_conversation.objectid];
// continue here
[messageQuery whereKey:#"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];

Resources