Last active
December 20, 2015 06:19
-
-
Save software-mariodiana/6085291 to your computer and use it in GitHub Desktop.
Loading a file asynchronously to use as the data source for a UITableView.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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]; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment