自定义NSOperation并发的问题
想自己动手并发一个NSOperation,但是问题好大。
实现代码如下
#import "NSOperationA.h"
@interface NSOperationA()
{
BOOL isfihish;
BOOL isexcuting;
}
@end
@implementation NSOperationA
-(BOOL)isConcurrent{
return YES;
}
-(BOOL)isExecuting{
NSLog(@"isExecuting:%d",isexcuting);
return isexcuting;
}
-(BOOL)isFinished{
NSLog(@"isFinished:%d",isfihish);
return isfihish;
}
-(void)start{
if( [self isCancelled] ){
[self willChangeValueForKey:@"isFinished"];
isfihish = YES;
[self didChangeValueForKey:@"isFinished"];
NSLog(@"start- isCancelled");
}
else{
[self willChangeValueForKey:@"isExecuting"];
isexcuting = YES;
[self runLoop];
[self didChangeValueForKey:@"isExecuting"];
}
}
-(void)cancelSelf{
[self willChangeValueForKey:@"isFinished"];
isfihish = YES;
[self didChangeValueForKey:@"isFinished"];
}
-(void)runLoop{
while (![self isCancelled]) {
NSLog(@"runLoop");
[NSThread sleepForTimeInterval:1];
}
NSLog(@"runloop is canceled");
}
@end
调用代码如下:
//
NSOperationA*a;
////
a = [[NSOperationA alloc]init];
NSOperationQueue * q = [[NSOperationQueue alloc]init];
[q addOperation:a];
[self performSelector:@selector(cancelA) withObject:nil afterDelay:3];
//
-(void)cancelA
{
[a cancelSelf];
}
问题来了:
上面的代码打出来的log是这样的:
runloop
runloop
runloop
isFinished:1
runloop
runloop
N个runloop。
请问
1.为什么没有频繁的调用isFinished 跟isExecuting
2.为什么明明调用了cancelSelf,但是[self isCancelled] 依然是0.
如果将 [self runloop] 修改为[NSThread detachNewThreadSelector:@selector(runLoop) toTarget:self withObject:nil];
log如下:
runLoop
isExecuting:1
isFinished:0
runLoop
runLoop
isFinished:1
runLoop
runLoop
runLoop
问题:
1.为何这里会多了几次 isExecuting 跟 isFiished的调用?
实现代码如下
#import "NSOperationA.h"
@interface NSOperationA()
{
BOOL isfihish;
BOOL isexcuting;
}
@end
@implementation NSOperationA
-(BOOL)isConcurrent{
return YES;
}
-(BOOL)isExecuting{
NSLog(@"isExecuting:%d",isexcuting);
return isexcuting;
}
-(BOOL)isFinished{
NSLog(@"isFinished:%d",isfihish);
return isfihish;
}
-(void)start{
if( [self isCancelled] ){
[self willChangeValueForKey:@"isFinished"];
isfihish = YES;
[self didChangeValueForKey:@"isFinished"];
NSLog(@"start- isCancelled");
}
else{
[self willChangeValueForKey:@"isExecuting"];
isexcuting = YES;
[self runLoop];
[self didChangeValueForKey:@"isExecuting"];
}
}
-(void)cancelSelf{
[self willChangeValueForKey:@"isFinished"];
isfihish = YES;
[self didChangeValueForKey:@"isFinished"];
}
-(void)runLoop{
while (![self isCancelled]) {
NSLog(@"runLoop");
[NSThread sleepForTimeInterval:1];
}
NSLog(@"runloop is canceled");
}
@end
调用代码如下:
//
NSOperationA*a;
////
a = [[NSOperationA alloc]init];
NSOperationQueue * q = [[NSOperationQueue alloc]init];
[q addOperation:a];
[self performSelector:@selector(cancelA) withObject:nil afterDelay:3];
//
-(void)cancelA
{
[a cancelSelf];
}
问题来了:
上面的代码打出来的log是这样的:
runloop
runloop
runloop
isFinished:1
runloop
runloop
N个runloop。
请问
1.为什么没有频繁的调用isFinished 跟isExecuting
2.为什么明明调用了cancelSelf,但是[self isCancelled] 依然是0.
如果将 [self runloop] 修改为[NSThread detachNewThreadSelector:@selector(runLoop) toTarget:self withObject:nil];
log如下:
runLoop
isExecuting:1
isFinished:0
runLoop
runLoop
isFinished:1
runLoop
runLoop
runLoop
问题:
1.为何这里会多了几次 isExecuting 跟 isFiished的调用?
...全文
相关推荐
NSOperation例子
这有助于优化系统资源的使用,防止过多的并发导致性能问题。4.
iOS 多线程NSoperation
七、自定义NSOperation子类为了实现更复杂的功能,开发者可以创建自己的NSOperation子类。
Multi-ImagesDownload:利用NSOperation实现tableView中的多图片下载
**自定义NSOperation子类**: - 为了实现特定的图片下载逻辑,我们需要创建一个继承自NSOperation的子类。在这个子类中,我们可以重写`main`方法,实现图片的下载和处理逻辑。
iOS并发编程指南.pdf.zip
你可以创建自定义的NSOperation子类,或者直接使用苹果提供的NSInvocationOperation和NSBlockOperation。
IOS应用源码——NSOperationTest.rar
通过设置最大并发数,我们可以限制同一时间执行的操作数量,防止过多的并发导致性能问题。


