Before we talk about NSOperation, let’s talk about GCD. GCD technology is a lightweight magic technology that hides the underlying implementation. We can easily implement multithreaded programming with GCD and Block. NSOprationQueue, another multi-threaded programming technique, lets you queue background threads and provide more entry points, similar to the GCD implementation.
This similarity is not a coincidence. In the early days, both MacOX and iOS programs generally used Operation Queue to write background thread code, while the GCD technology that emerged later was basically implemented in accordance with the principle of the former. With the popularity of GCD, After iOS 4 and MacOS X 10.6, the underlying implementation of Operation Queue is implemented in GCD.
Therefore, it is now possible to use the encapsulation on top of the Operation Queue to achieve simpler multithreading operations.
In the case of multiplexing controls, or multitasking, it is inevitable to start multiple threads and interrupt threads.
At this point, we can use NSOperation to asynchronously execute and interrupt tasks. Including IOS UITableView and UICollectionView in the cell reuse state of multithreading operations
@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; 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]; * / * 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]; }} // asynchronous task - (NSBlockOperation *)operationEvent:(NSInteger)index {WEAKSELF NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ }]; return operation; } - (void)dealloc {NSLog(@" free %@",self); [self.operationQueue cancelAllOperations]; }Copy the code
Interrupts and enables tasks in hide and show reuse controls. A variety of complex tasks can be handled under the current control without conflict. These are useful in many situations, such as image loading, image compression, download callback, and asynchronous reading of resources.