Loading a file asynchronously to use as the data source for a UITableView.
// Let's not block while loading data from a file
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *path = [[NSBundle mainBundle] pathForResource:@"word_list" ofType:@"txt"];
NSError *error;
NSString *contents =
[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
self.tableData = [contents componentsSeparatedByString:@"\n"];
dispatch_async(dispatch_get_main_queue(), ^{
// Take care of anything we need to do on the main thread
if (error) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Error loading data"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
}
[[self tableView] reloadData];
});
});