国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

NSOPeration NSOperationQueue

2019-11-09 17:57:39
字體:
供稿:網(wǎng)友

NSOperation是一個抽象類。實際開發(fā)中用它的兩個子類:NSInvocationOperation NSBlockOperation

//NSBlockOperation可以通過block異步執(zhí)行任務(wù),且block里面的代碼是同步的- (void)blockOperationTest{ NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"1在第%@個線程",[NSThread currentThread]); NSLog(@"1haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"2在第%@個線程",[NSThread currentThread]); NSLog(@"2haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"3在第%@個線程",[NSThread currentThread]); NSLog(@"3haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"4在第%@個線程",[NSThread currentThread]); NSLog(@"4haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"5在第%@個線程",[NSThread currentThread]); NSLog(@"5haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"6在第%@個線程",[NSThread currentThread]); NSLog(@"6haha"); }];// [blockOperation start]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:blockOperation];}//不放大queue里面在主線程同步執(zhí)行

NSInvocationOperation 類似于button添加點擊事件

- (void)invocationTest{ NSInvocationOperation *invo = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testNSOperation) object:nil]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:invo]; //不放大queue里面在主線程同步執(zhí)行}

添加依賴關(guān)系是Queue的最大特征

- (void)relationTest{ NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation1) object:nil]; op1.completionBlock = ^(){ }; NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation2) object:nil]; op2.completionBlock = ^(){ }; NSInvocationOperation *op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation3) object:nil]; NSInvocationOperation *op4 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation4) object:nil]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [op2 addDependency:op1]; // 操作2依賴于操作1 [op3 addDependency:op2]; [op4 addDependency:op3]; [queue addOperation:op4]; [queue addOperation:op3]; [queue addOperation:op2]; [queue addOperation:op1];}

ios 針對http請求失效:https://segmentfault.com/a/1190000002933776 繼承NSOperation 的圖片請求operation類,只需要重新寫main 方法

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@PRotocol SDDownloadOperationDelegate <NSObject>- (void)downloadFinishWithImage:(UIImage *)image;@end@interface SDDownloadOperation : NSOperation@property (nonatomic, copy) NSString *imageURL;@property (nonatomic, weak) id <SDDownloadOperationDelegate> delegate;- (instancetype)initWithUrl:(NSString *)url delegate:(id<SDDownloadOperationDelegate>)delegate;@end#import "SDDownloadOperation.h"@implementation SDDownloadOperation- (instancetype)initWithUrl:(NSString *)url delegate:(id<SDDownloadOperationDelegate>)delegate{ self = [super init]; if (!self) { return nil; } self.imageURL = url; self.delegate = delegate; return self;}- (void)main{ if (self.isCancelled) { return; } NSURL *url = [NSURL URLWithString:self.imageURL]; NSData *imageData = [NSData dataWithContentsOfURL:url]; if (self.isCancelled) { url = nil; imageData = nil; return; } UIImage *image = [UIImage imageWithData:imageData]; if (self.isCancelled) { imageData = nil; return; } if (self.delegate && [self.delegate respondsToSelector:@selector(downloadFinishWithImage:)]) { [self.delegate downloadFinishWithImage:image]; }}@end
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武定县| 绥宁县| 西安市| 建昌县| 衡水市| 林西县| 米易县| 溧阳市| 曲沃县| 宾阳县| 哈巴河县| 五大连池市| 双柏县| 阿勒泰市| 水富县| 景德镇市| 明星| 福泉市| 图们市| 毕节市| 道孚县| 嘉荫县| 客服| 阳朔县| 泾阳县| 襄垣县| 赤水市| 德令哈市| 阜南县| 阿坝县| 左权县| 朝阳区| 名山县| 易门县| 玛曲县| 辰溪县| 页游| 东平县| 双流县| 巧家县| 香格里拉县|