如何使用NSOperationQueue创建2个操作(How to create 2 operations using NSOperationQueue)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
如何使用NSOperationQueue创建2个操作(How to create 2 operations using NSOperationQueue)

我想创建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.

更多推荐

本文发布于:2023-04-24 12:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/cefb2fcf90ae9969e2d11d12a300b25a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   操作   operations   NSOperationQueue   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!