Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created January 26, 2011 08:19
Show Gist options
  • Save marshluca/796412 to your computer and use it in GitHub Desktop.
Save marshluca/796412 to your computer and use it in GitHub Desktop.
使用NSInvocationOperation 处理多线程
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Load"
style:UIBarButtonItemStyleDone
target:self
action:@selector(loadData)];
NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000];
self.array = _array;
[_array release];
}
- (void) loadData {
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(loadDataWithOperation)
object:nil];
[queue addOperation:operation];
[operation release];
}
- (void) loadDataWithOperation {
NSURL *dataURL = [NSURL URLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"];
NSArray *tmp_array = [NSArray arrayWithContentsOfURL:dataURL];
for(NSString *str in tmp_array) {
[self.array addObject:str];
}
[self.tableView reloadData];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment