How can I save data from parse to an NSArray
in the below format. which is being used to populate a UITableView
, which is to be used for a search bar.
_candyArray = [NSArray arrayWithObjects:
[Candy candyOfCategory:@"chocolate" name:@"chocolate bar" mName:@"test1"],
[Candy candyOfCategory:@"chocolate" name:@"chocolate chip" mName:@"test1"],
[Candy candyOfCategory:@"chocolate" name:@"dark chocolate" mName:@"test1"],
[Candy candyOfCategory:@"hard" name:@"lollipop" mName:@"test1"],
[Candy candyOfCategory:@"hard" name:@"candy cane" mName:@"test1"],
[Candy candyOfCategory:@"hard" name:@"jaw breaker" mName:@"test1"],
[Candy candyOfCategory:@"other" name:@"caramel" mName:@"test1"],
[Candy candyOfCategory:@"other" name:@"sour chew" mName:@"test1"],
[Candy candyOfCategory:@"other" name:@"peanut butter cup" mName:@"test1"],
[Candy candyOfCategory:@"other" name:@"gummi bear" mName:@"test1"], nil];
I tried the below code, but id didn't load any data, keeps returning the value as null
when I run it through NSLog
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"Name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
{
self.arrayName = objects;
}
}else {
NSLog(@"Error, %@ %@",error,[error userInfo]);
}
}];
NSLog(@"Array : %@", arrayName);
Is there anyway to save the data in the above type of array?
The reason it is null is that the
findObjectsInBackgroundWithBlock
is not finished when that log statement is run. If you want to handle the results (or log them), you need to put the log statement inside the block, or you can use
findObjectsInBackgroundWithTarget:selector:
instead, and have the handling code in your callback method.
Where are your candy objects coming from? If it is a custom class, then it is likely not saving to parse in the first place.
Here's one way to use your existing data, but perhaps it's better to create a PFObject subclass. (Check in the docs for that)
Ok, next step is retrieving them. (basically rajjjjj's example w/ a few changes)
Then, if you want to save them back into an array as your question originally stated
You have not allocated the array i think so change your code as follows