我想创建2个同时运行的操作。 一个人不断创建一个对象,并以15毫秒的间隔将其添加到队列中。 另一个操作以10毫秒的间隔连续从队列中删除第一个项目。
- (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; _arrInformations = [[NSMutableArray alloc] init]; queue = [NSOperationQueue new]; // start continuous processing [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(addNewInformation) userInfo:nil repeats:YES]; [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(removeInformation) userInfo:nil repeats:YES]; } -(void)addNewInformation { NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addDataWithOperation) object:nil]; /* Add the operation to the queue */ [queue addOperation:operation]; } - (void)removeInformation { NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(removeDataWithOperation) object:nil]; /* Add the operation to the queue */ [queue addOperation:operation]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; } - (void) addDataWithOperation { NSLog(@"Add data"); [_arrInformations addObject:@"Informations"]; } - (void) removeDataWithOperation { if (_arrInformations.count) { [_arrInformations removeLastObject]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; } }非常感谢!
I want to create 2 operations run simultaneously. One continually creates an object and add that to a queue at 15 ms interval. Another operation continually removes the 1st item from queue at 10 ms interval.
- (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; _arrInformations = [[NSMutableArray alloc] init]; queue = [NSOperationQueue new]; // start continuous processing [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(addNewInformation) userInfo:nil repeats:YES]; [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(removeInformation) userInfo:nil repeats:YES]; } -(void)addNewInformation { NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addDataWithOperation) object:nil]; /* Add the operation to the queue */ [queue addOperation:operation]; } - (void)removeInformation { NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(removeDataWithOperation) object:nil]; /* Add the operation to the queue */ [queue addOperation:operation]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; } - (void) addDataWithOperation { NSLog(@"Add data"); [_arrInformations addObject:@"Informations"]; } - (void) removeDataWithOperation { if (_arrInformations.count) { [_arrInformations removeLastObject]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; } }Thanks a lot!
最满意答案
我认为您不需要使用NSOperationQueue来实现此功能。 Grand Central Dispatch和Blocks应该为您提供所需的功能。
- (void)viewDidLoad { [super viewDidLoad]; _arrInformations = [[NSMutableArray alloc] init]; _shouldContinue = YES; // set this to NO on viewWillDisappear to stop the dispatching [self addNewInformation]; } -(void)addNewInformation { [_arrInformations addObject:@"Informations"]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; if (_shouldContinue) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self removeInformation]; }); } - (void)removeInformation { [_arrInformations removeObject:@"Informations"]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; if (_shouldContinue) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self addInformation]; }); }您应该注意,每隔10-15毫秒调用-reloadData对iOS性能非常重要 。 您应该将NSNotificationCenter和KVO视为响应数据更改的替代方法,而不是像这样在循环中导致它们。
I don't think you need to use an NSOperationQueue to realize this functionality. The Grand Central Dispatch and Blocks should get you the functionality that you need.
- (void)viewDidLoad { [super viewDidLoad]; _arrInformations = [[NSMutableArray alloc] init]; _shouldContinue = YES; // set this to NO on viewWillDisappear to stop the dispatching [self addNewInformation]; } -(void)addNewInformation { [_arrInformations addObject:@"Informations"]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; if (_shouldContinue) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self removeInformation]; }); } - (void)removeInformation { [_arrInformations removeObject:@"Informations"]; [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; if (_shouldContinue) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self addInformation]; }); }You should note that calling -reloadData every 10-15 ms is going be VERY taxing on iOS performance. You should look at NSNotificationCenter and KVO as alternative methods of responding to data changes, instead of causing them in a loop like this.
更多推荐
发布评论