How can I populate a UITableView with data from a parse.com request after it runs in the background?

I have a UITableViewController that is responsible for displaying a table full of employees. This data is stored in a database at parse.com.

This is my UITableViewController in which I just start the repository:

 -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nil bundle:nil]; if(self){ store = [[EmployeeStore alloc] init]; } return self; } 

This is the EmployeeStore init method in which I request employees:

 -(id) init{ self = [super init]; if(self){ employees = [[NSMutableArray alloc] init]; [self fetchEmployeesFromDatabase]; } return self; } 

fetchEmployeesFromDatabase , where I request employees.

 -(void) fetchEmployeesFromDatabase{ PFQuery *query = [PFQuery queryWithClassName:@"Employee"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d scores.", objects.count); // Do something with the found objects for (PFObject *object in objects) { NSLog(@"%@", object.objectId); [employees addObject:object]; } } else { // Log details of the failure NSLog(@"Error: %@ %@", error, [error userInfo]); } }]; } 

I successfully receive them, however, the problem is that the query is executed in the background and does not end until it is loaded after viewing the table, so the table view is not filled. I need the table to reload its data after the query is complete, but how do I do this?

+6
source share
5 answers

UITableViewController

 -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nil bundle:nil]; if(self){ store = [[EmployeeStore alloc] init]; store.tableView = self.tableView; } return self; } 

EmployeeStore.h

 @property (nonatomic, strong) UITableView *tableView; 

EmployeeStore.m

 -(id) init{ self = [super init]; if(self){ employees = [[NSMutableArray alloc] init]; [self fetchEmployeesFromDatabase]; } return self; } 

and fetchEmployeesFromDatabase

 -(void) fetchEmployeesFromDatabase{ PFQuery *query = [PFQuery queryWithClassName:@"Employee"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d scores.", objects.count); // Do something with the found objects for (PFObject *object in objects) { NSLog(@"%@", object.objectId); [employees addObject:object]; } dispatch_async(dispatch_get_main_queue(), ^ { [self.tableView reloadData]; }); } else { // Log details of the failure NSLog(@"Error: %@ %@", error, [error userInfo]); } }]; } 
+11
source

When the download is complete, let's say that the table view is reloaded.

 if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d scores.", objects.count); // Do something with the found objects for (PFObject *object in objects) { NSLog(@"%@", object.objectId); [employees addObject:object]; } dispatch_async(dispatch_get_main_queue(), ^ { [tableView reloadData]; }); } else { 

You need to make sure that your employees are stored in streaming mode or not available on the background and main streams at the same time.

+2
source
 -(void) fetchEmployeesFromDatabase{ PFQuery *query = [PFQuery queryWithClassName:@"Employee"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d scores.", objects.count); // Do something with the found objects for (PFObject *object in objects) { NSLog(@"%@", object.objectId); [employees addObject:object]; } } else { // Log details of the failure NSLog(@"Error: %@ %@", error, [error userInfo]); } [self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:NO]; }]; } -(void)reloadTableView{ [self.aTableview reloadData]; } 

Now you load the column on mainthread. Try it.

0
source

Here, the data is populated in the table before the completion of the table view. All you have to do is place the [self fetchEmployeesFromDatabase] statement; inside the viewDidLoad () method or any method that invokes the creation of your table.

0
source

I had this problem besides the collection. After a lot of pain, it did the trick to reload data through the main thread.

 dispatch_async(dispatch_get_main_queue(), ^ { [self.collectionView reloadData]; [self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]]; }); 
0
source

Source: https://habr.com/ru/post/948189/


All Articles