生活随笔
收集整理的這篇文章主要介紹了
IOS基础NSOperation的操作优先级和操作依赖
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS基礎NSOperation的操作優先級和操作依賴
#import "ViewController.h"
@interface ViewController
()
@property(nonatomic
,strong
)NSOperationQueue
*queue
;
@end
@implementation ViewController
-(NSOperationQueue
*)queue
{if(_queue
==nil
){_queue
= [[NSOperationQueue alloc
] init
];}return _queue
;
}
- (void)viewDidLoad
{[super viewDidLoad
];NSBlockOperation
*op1
= [NSBlockOperation blockOperationWithBlock
:^{for(int i
=0;i
<20;i
++){NSLog(@"op1 = %d",i
);}}];op1
.qualityOfService
= NSQualityOfServiceUserInteractive
;[self.queue addOperation
:op1
];[op1 setCompletionBlock
:^{NSLog(@"===========op1 操作完成執行的=========%@",[NSThread currentThread
]);}];NSBlockOperation
*op2
= [NSBlockOperation blockOperationWithBlock
:^{for(int i
=0;i
<20;i
++){NSLog(@"op2 = %d",i
);}}];op2
.qualityOfService
= NSQualityOfServiceBackground
;[self.queue addOperation
:op2
];
}
@end
#import "ViewController.h"@interface ViewController
()
@property(nonatomic
,strong
)NSOperationQueue
*queue
;
@end
@implementation ViewController
-(NSOperationQueue
*)queue
{if(_queue
==nil
){_queue
= [[NSOperationQueue alloc
] init
];}return _queue
;
}
- (void)viewDidLoad
{[super viewDidLoad
];NSBlockOperation
*op1
= [NSBlockOperation blockOperationWithBlock
:^{NSLog(@"下載");}];NSBlockOperation
*op2
= [NSBlockOperation blockOperationWithBlock
:^{[NSThread sleepForTimeInterval
:2.0];NSLog(@"解壓");}];NSBlockOperation
*op3
= [NSBlockOperation blockOperationWithBlock
:^{NSLog(@"升級完成");}];[op2 addDependency
:op1
];[op3 addDependency
:op2
];[self.queue addOperations
:@[op1
,op2
] waitUntilFinished
:NO
];[[NSOperationQueue mainQueue
] addOperation
:op3
];
}@end
總結
以上是生活随笔為你收集整理的IOS基础NSOperation的操作优先级和操作依赖的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。