Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mike Lee
6,579 PointsHow to get array outside of findObjectsInBackgroundWithBlock
Hey guys Ive been racking my brain for days now. How do i get the array from the results outside of the block so I can put some data into a table?
4 Answers
Stone Preston
42,016 Pointsyou can create an NSArray property in your class's header file
@property (nonatomic, strong) NSArray *myArray;
then in the .m file where you call that method set your property using the objects parameter
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
self.myArray = objects;
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
//myArray now contains the objects from the query and can be accessed outside the block
NSLog(@"myArray: %@", self.myArray);
you would use tableView delegate methods to actually use the array as the data source, and you would typically reload the table whenever the query is run
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
self.myArray = objects;
//reload the table after getting new data
[self.tableView reloadData];
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
Mike Lee
6,579 PointsIm still getting a null outside of the block
Mike Lee
6,579 Pointsany other ideas Stone Preston ?
Mike Lee
6,579 PointsAnybody?