Ios 多线程之NSOperation与NSOprationQueue

本文探讨了NSOperation与GCD在iOS多线程编程中的作用及其实现方式。NSOperation提供了队列式的后台线程管理机制,而GCD则是一种轻量级的多线程解决方案。文章还介绍了如何使用NSOperationQueue和NSBlockOperation来管理UICollectionView中的复用cell任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在说NSOperation之前,先说一下gcd,gcd 技术是一个轻量的,底层实现隐藏的神奇技术,我们能够通过gcd和block轻松实现多线程编程,有时候,gcd相比其他系统提供的多线程方法更加有效,当然,有时候gcd不是最佳选择,另一个多线程编程的技术 NSOprationQueue 让我们能够将后台线程以队列方式依序执行,并提供更多操作的入口,这和 gcd 的实现有些类似。

这种类似不是一个巧合,在早期,MacOX 与 iOS 的程序都普遍采用Operation Queue来进行编写后台线程代码,而之后出现的gcd技术大体是依照前者的原则来实现的,而随着gcd的普及,在iOS 4 与 MacOS X 10.6以后,Operation Queue的底层实现都是用gcd来实现的。

所以,目前可以利用Operation Queue上层的封装,比较简易的实现更简单的多线程操作。

在复用控件,或者多任务执行的情况下,避免不了要开启多个线程和中断线程。

此时,我们就可以使用NSOperation来异步执行任务和中断任务。 包括IOS UITableView和UICollectionView中的cell复用状态下的多线程操作


@property (strong, nonatomic) NSOperationQueue *operationQueue;
@property (strong, nonatomic) NSMutableDictionary *operationDict;



- (NSMutableDictionary *)operationDict {
  if (!_operationDict) {
    _operationDict = [NSMutableDictionary dictionary];
  }
  return _operationDict;
}

- (NSOperationQueue *)operationQueue {
  if (!_operationQueue) {
    _operationQueue = [[NSOperationQueue alloc] init];
    _operationQueue.maxConcurrentOperationCount = 6;
  }
  return _operationQueue;
}

//控件显示开启任务
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

  if (![self.operationDict objectForKey:@(indexPath.item).stringValue]) {
    NSBlockOperation *operation = [self operationEvent:indexPath.item];
    [self.operationQueue addOperation:operation];
    [self.operationDict setObject:operation forKey:@(indexPath.item).stringValue];
  }
}

//控件消失中断任务
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

  if (self.operationDict[@(indexPath.item).stringValue]) {
    NSBlockOperation *operation = self.operationDict[@(indexPath.item).stringValue];
    [operation cancel];
    [self.operationDict removeObjectForKey:@(indexPath.item).stringValue];
  }
}

//异步任务
- (NSBlockOperation *)operationEvent:(NSInteger)index {
  WEAKSELF
  NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

  }];
  return operation;
}

- (void)dealloc
{
  NSLog(@"释放%@",self);
  [self.operationQueue cancelAllOperations];
}

在隐藏和显示复用控件中中断和开启任务。可以在当前控件下处理各种复杂任务而不会冲突。例如图片加载,图片压缩,下载回调,异步读取资源等多种情况下都非常实用。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值