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

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

NSOPeration NSOperationQueue

2019-11-09 18:52:05
字體:
供稿:網(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ā)表
主站蜘蛛池模板: 吉林省| 和龙市| 清水河县| 盱眙县| 湖南省| 江川县| 嵊泗县| 富顺县| 德昌县| 屯昌县| 洛南县| 若尔盖县| 柳江县| 大洼县| 岐山县| 博白县| 瑞昌市| 四子王旗| 遂平县| 潍坊市| 禹城市| 扎鲁特旗| 茂名市| 顺昌县| 纳雍县| 绥滨县| 万全县| 原阳县| 成都市| 临颍县| 木兰县| 芒康县| 澄迈县| 溧阳市| 铜川市| 镶黄旗| 崇仁县| 珠海市| 呈贡县| 彩票| 嵊泗县|