How to get the objectId from NSArray from PFquery in PARSE.com - ios

My PFQuery returns the following description.I am trying to fetch the objectId of the class
YouQuestions.For eg: in the below description Ovx3B1TnaC is the objectId for the first index of quesAray. But I have no idea on how to fetch it.
Printing description of quesArray:
<__NSArrayM 0xe1198f0>(
<YouQuestions:OVx3BlTnaC:(null)> {
askedUser = "<PFUser:XGvZsNyg9p>";
attachedImage = "<PFFile: 0xb4c9d20>";
category = Business;
geoLocation = "<PFGeoPoint: 0xb4c9ea0>";
question = "Who is kaka?";
thumbImage = "<PFFile: 0xb4c9dd0>";
},
This is how I did but returned nil
PFQuery *fetchTimeLine = [PFQuery queryWithClassName:#"YouQuestions"];
[fetchTimeLine whereKeyExists:#"objectId"];
[fetchTimeLine findObjectsInBackgroundWithBlock:^(NSArray *quesArray, NSError *error)
{
for (int i =0; i<quesArray.count; i++)
{
PFObject *obj = [quesArray[i] objectForKey:#"objectId"];
[searchobjectIDsArray addObject:obj.objectId];
}
}];
EDIT:
I fixed it like this
for (PFObject *object in quesArray) {
NSLog(#"%#", object.objectId);
}

to get the array of ids:
NSArray *oids = [quesArray valueForKeyPath:#"objectId"];

Related

How can i take this array and make into one array?

PFQuery *query = [PFQuery queryWithClassName:#"Category"];
// Retrieve the object by id
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the cloud. playerName hasn't changed.
// NSLog(#"%#",[objects objectAtIndex:1]);
// NSLog(#"%#",[objects valueForKey:#"mensClothingType"]);
NSArray * result = [[objects valueForKey:#"mensClothingType"] componentsJoinedByString:#""];
NSLog(#"%#",result);
}];
}
how can i get all of the 'mensClothingType' from objects and put that into one array? currently if i read out the array 'objects'
i get this:
(
(
Shirts
),
(
Jeans
)
)
NSMutableArray* resultArray = [NSMutableArray new];
for(NSArray* innerArray in outerArray)
{
for(id object in innerArray)
{
[resultArray addObject:object];
}
}

Sort Parse Objects by Number

I have a few objects in a Parse database, a few are as shown below. I would like to sort these objects by highScore, which is stored as a number.
(
"<Score: 0x7fee53740700, objectId: zMjL3eNWpI, localId: (null)> {\n Score = \"High Score: 60\";\n TeamName = \"Team0\";\n highScore = 60;\n}",
"<Score: 0x7fee534b5080, objectId: nysaJjYsth, localId: (null)> {\n Score = \"High Score: 86\";\n TeamName = Team1;\n highScore = 86;\n}",
"<Score: 0x7fee535f6ad0, objectId: 7Hj8RP4wYD, localId: (null)> {\n Score = \"High Score: 23\";\n TeamName = Team2;\n highScore = 23;\n}"
)
I have the following code which I loop over the objects and pull out the Number highScore for each object, but I am not sure how to continue. Is there a way I can return (NSComparisonResult)NSOrderedAscending? If anyone has any advice please let me know. Thanks.
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];//class name is Score
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (int i = 0; i<=objects.count; i++){
NSArray *array = [objects objectAtIndex:i];//Selects the first "object" from all the "objects"
NSNumber *test= [objects objectAtIndex:i];
array = [array valueForKey:#"highScore"];
test = [test valueForKey:#"highScore"];
test1 = [test intValue];//test1 is the current database object highScore for the current object
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Parse.com offers a great iOS SDK that already gives you out of the box what you are looking for. When you make a PFQuery, Parse gives you the option to order the results the way you would like. I believe you should try this:
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:#"highScore"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(#"High Score is %d", [object["highScore"]intValue]);
}
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];

How can i perform a PFRelation query using parse in ios

I have created a relation between two tables
LocationClass Table In this table i have a column with location images with the relation of another table ie., Assets
![This is location Class Image, If user selected a locationName, need to get the locationImages(viewRelation)][1]
Assets Table, It contains Images for each relation Now my query is how to get the data from relation database. Here if user select a location means i need to get a group of images for the relevant location ![This is my Assets Table, here need to retrieve the images based on the selected location from the locationClass Table][2]
Till now, I have done with this format
PFQuery *queryObj = [PFQuery queryWithClassName:#"LocationClass"];
sharedDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
// Run the query
[queryObj whereKey:#"locationImages" equalTo:[PFObject objectWithoutDataWithClassName:#"Assets" objectId:#"aAPzhdO4w6"]];
[queryObj findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
[locationArray addObjectsFromArray:objects];
sharedDelegate.locationsArray = locationArray;
[locationDropDown reloadData];
}
}];
In my location array for a single object contain
<__NSArrayM 0xaee4eb0>(
<LocationClass:ZtOP9voUak:(null)> {
LocationId = WilliamsBurgId;
LocationName = WilliamsBurg;
locationImages = "<PFRelation: 0xaf53c30>(<00000000>.(null) -> Assets)";
}
)
Solution for to pass the data from PFRelation Object
// should pass the main table name
PFQuery *query = [PFQuery queryWithClassName:#"LocationClass"];
// should pass object id for the selected row
PFObject *getImageObject = [query getObjectWithId:#"need to pass object id from the maintable"];
locationImagesArray = [[NSMutableArray alloc]init];
// To acess the data from the relation object
PFRelation *relationObj = [getImageObject relationForKey:#"locationImages"];
PFQuery *query1 = [relationObj query];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
[locationImagesArray addObjectsFromArray:results];
for (int imgCount = 0; imgCount < [locationImagesArray count]; imgCount ++) {
PFFile *getImage1 = [[locationImagesArray valueForKey:#"Image"] objectAtIndex:imgCount];
[getImage1 getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
{
if (imageData!=nil) {
UIImage *image = [UIImage imageWithData:imageData];
activityImage.image = image;
NSLog(#"location image output");
NSLog(#"location image output : %#", activityImage.image);
}
}];
}

Get Array With Array but it's goes array in array

I have data on database like this.
["{37.331622, -122.030337}","{37.331593, -122.03051}","{37.331554, -122.030681}","{37.331383, -122.030757}","{37.33108, -122.030772}","{37.330798, -122.030729}","{37.330636, -122.030636}"]
Then i try to query data from database by following code.
- (void)updateLocations {
CGFloat kilometers = self.radius/1000.0f;
//PFUser *user = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Session"];
[query whereKey:#"objectId" equalTo:#"t2udAri048"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"objects %#",objects);
NSLog(#"path %#",[objects valueForKey:#"Path"]);
NSArray *pointsArray = [objects valueForKey:#"Path"];;
NSInteger pointsCount = pointsArray.count;
CLLocationCoordinate2D pointsToUse[pointsCount];
for(int i = 0; i < pointsCount; i++) {
CGPoint p = CGPointFromString(pointsArray[i]);
pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
}
MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
[self.mapView addOverlay:myPolyline];
//NSLog(#"Drawed %#",pointsArray);
}
}];
}
I get the value of [objects valueForKey:#"Path"]
(
(
"{37.331622, -122.030337}",
"{37.331593, -122.03051}",
"{37.331554, -122.030681}",
"{37.331383, -122.030757}",
"{37.33108, -122.030772}",
"{37.330798, -122.030729}",
"{37.330636, -122.030636}"
) )
But i want it to
(
"{37.331622, -122.030337}",
"{37.331593, -122.03051}",
"{37.331554, -122.030681}",
"{37.331383, -122.030757}",
"{37.33108, -122.030772}",
"{37.330798, -122.030729}",
"{37.330636, -122.030636}"
)
What should i do?
It looks as if objects is an array of one element, which is an object with the "Path"
property. In that case you should replace
NSArray *pointsArray = [objects valueForKey:#"Path"];
by
NSArray *pointsArray = [objects[0] valueForKey:#"Path"];
In your example you have the objectId. If that is the case, you use getObjectWithId. This will return only the object you want.
PFQuery *query = [PFQuery queryWithClassName:#"Session"];
[query getObjectWithId:#"hr46gjh45"];
Your example returns an array of objects; in this case the array has only one object (but it is still an array). In that case you must first retrieve the object from the array.

Extracting data from a Parse.com class and into an NSArray

I'm using the following query to get some data from a Parse.com class.
What I would like to do is extract the Rating object from the NSArray rateObjects. How would I go about doing this?
thanks for any help
PFQuery *rateQuery = [PFQuery queryWithClassName:#"Rating"];
[rateQuery whereKey:#"photo" equalTo:self.photo];
[rateQuery includeKey:#"photo"];
rateQuery.cachePolicy = kPFCachePolicyNetworkElseCache;
rateQuery.limit = 20;
[rateQuery findObjectsInBackgroundWithBlock:^(NSArray *rateObjects, NSError *error)
{
if( !error )
{
NSLog(#"rateObject %#", rateObjects);
}
}
];
Here's the NSLog output:
rateObject (
"<Rating:w9ENTO29mA:(null)> {\n ACL = \"<PFACL: 0x1e0a5380>\";\n Rating = 4;\n fromUser = \"<PFUser:uV2xu0c3ec>\";\n photo = \"<Photo:Rv4qqrHUPr>\";\n toUser = \"<PFUser:uV2xu0c3ec>\";\n user = \"<PFUser:uV2xu0c3ec>\";\n}",
"<Rating:t3pjtehYR0:(null)> {\n ACL = \"<PFACL: 0x1e0f9f90>\";\n Rating = 5;\n fromUser = \"<PFUser:uV2xu0c3ec>\";\n photo = \"<Photo:Rv4qqrHUPr>\";\n toUser = \"<PFUser:uV2xu0c3ec>\";\n user = \"<PFUser:uV2xu0c3ec>\";\n}"
)
Your NSArray will contain PFObjects, which you can treat in a similar way to a dictionary. In the query you ran above you received two rating objects back. If that's not what you wanted (you only wanted a single object) you may want to revisit how you're querying your data.
Assuming your Rating class in Parse contains a key called Rating you would access it like this:
[rateObject objectForKey:#"Rating"]
You can also use the new modern literal syntax if you like - rateObject[#"rating"]
You'll need to iterate through your array to view all the rating objects that have been returned, so you'll probably end up with something like this:
for (id item in rateObjects) {
int ratingVal = [[item objectForKey:#"Rating"] intValue];
NSLog(#"Rating: %d", ratingVal);
}
You may find Parse's iOS documentation helpful - and if you're not sure what the code above actually does, you may want to review arrays and how they work in Objective-C.
Try to use this:
[rateQuery findObjectsInBackgroundWithBlock:^(NSArray *rateObjects, NSError *error)
{
if( !error )
{
NSMutableArray *data = [[NSMutableArray alloc]init];
[data addObjectsFromArray:rateObjects];
NSArray *rating_data = [data valueForKey:#"Rating"];
NSLog(#"%#",[rating_data objectAtIndex:0]);
}
}
];
I hope this will help you.

Resources