以前写过一篇介绍NSInvocationOperation的文章:多线程之NSInvocationOperation,这篇文章是基于此的一个例子。
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSMutableArray *array;
}
@property (nonatomic, retain) NSMutableArray *array;
@end
RootViewController.m
#import "RootViewController.h"
@implementation RootViewController
@synthesize array;
- (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 performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell.textLabel setText:[self.array objectAtIndex:indexPath.row]];
return cell;
}
- (void)dealloc {
[super dealloc];
[array release];
}
@end
分享到:
相关推荐
OC-优先级NSInvocationOperation
OC-NSInvocationOperation
OC-操作间的依赖NSInvocationOperation
OC-监听操作是否结束NSInvocationOperation
iphone开发之多线程NSThread和NSInvocationOperation 的 使用
NSInvocationOperationViewController: NSOperation 的子类 NSInvocationOperation 线程的基本使用 NSBlockOperationViewController: NSOperation 的子类 NSBlockOperation 线程的基本使用 ...
1> Foundation框架提供了两个具体子类直接供我们使用:NSInvocationOperation和NSBlockOperation 2> 自定义子类继承NSOperation,实现内部相应的方法 2.执行操作 NSOperation调用start方法即可开始执行操作,...
创建NSInvocationOperation对象: @interface OQCreateInvocationOperation : NSObject- ( NSInvocationOperation *) invocationOperationWithData : ( id ) data ;- ( NSInvocationOperation *) ...