Reputation: 1042
Below is the parse query, i am trying to display the categories in a collection view, but the numberOfItemsInSection
method runs before getCategories
has the time to pull information from parse. numberOfItemsInSection
uses getCategories _anArrayOfCategories
to return the number of categories in the collection view.
-(void)getCategories{
[super viewWillAppear:animated]; //calls retrieve messages method below
//get Categories where the class name is Categories
PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
//- (void)selectKeys:(NSArray *)keys
[query selectKeys:@[@"CName"]];
//[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
_anArrayOfCategories = [[NSArray alloc] initWithArray:objects];
NSLog(@"Test 1: Retrieved %lu Categories", (unsigned long)[_anArrayOfCategories count]);
}
}];
}
any suggestions?
Upvotes: 0
Views: 65
Reputation: 6396
Either update your table after the query finishes, or make the query on a previous controller and push to this controller after it completes. Remember that these are UI operations and need to be done on the main thread.
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
_anArrayOfCategories = [[NSArray alloc] initWithArray:objects];
NSLog(@"Test 1: Retrieved %lu Categories", (unsigned long)[_anArrayOfCategories count]);
dispatch_async(dispatch_get_main_queue(), ^{ // UI operations on the main thread
[self.tableView reloadData];
});
}
}];
Edit: Just to make sure, as it's unclear from your post, you don't want to call this method from numberOfRowsInSection
. Put it in viewDidLoad
or something similar, and then have numberOfRowsInSection
use the _anArrayOfCategories
object.
Upvotes: 1