AsyncSocket 源码分析

CocoaAsyncSocket 源码学习摘要:

1. [NSObject cancelPreviousPerformRequestsWithTarget:self]

//AsyncSocket 的dealloc说明。
- (void)dealloc
{
    [self close];
    [theReadQueue release];
    [theWriteQueue release];
    [theRunLoopModes release];
    [partialReadBuffer release];
    [NSObject cancelPreviousPerformRequestsWithTarget:theDelegate selector:@selector(onSocketDidDisconnect:) object:self];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [super dealloc];
}

一般我们都知道调用了[instance performSelector:@selector() withObject:nil afterDelay:]会导致instance引用计数+1。如果我们调用了[instance performSelector:@selector() withObject:nil afterDelay:]。在某些情况下我们就需要调用[NSObject cancelPreviousPerformRequestsWithTarget:instance]或者功能类似API,来取消延迟。否则有些时候会造成内存问题(实例都释放了还在调用实例方法)导致crash,或者导致某个类延迟释放。

问题:
如果有一个类,在类中有一个方法调用了[self performSelector:@selector() withObject:nil afterDelay:]。那么在这个类中对应的dealloc方法中是否有必要调用[NSObject cancelPreviousPerformRequestsWithTarget:self]呢?

因为延迟调用会把self引用计数+1,调用了之后,只能等延迟方法执行完或者被[NSObject cancelPreviousPerformRequestsWithTarget:self]类似方法取消掉,否则不可能执行到dealloc方法中。这样看来dealloc方法调用[NSObject cancelPreviousPerformRequestsWithTarget:self]没啥用。
但是最佳实践中我还是会建议在dealloc方法最后加上[NSObject cancelPreviousPerformRequestsWithTarget:self]
理由如下:

如果我们在定义一个类,里面有方法调用了[self performSelector:@selector() withObject:nil afterDelay:]。那么在这个类的dealloc方法最后要调用[NSObject cancelPreviousPerformRequestsWithTarget:self]。因为dealloc里面一旦调用了一些方法,这些方法间接或者直接调用了[self performSelector:@selector() withObject:nil afterDelay:],那么就等待着crash吧。此时在dealloc方法最后加上[NSObject cancelPreviousPerformRequestsWithTarget:self]。问题就解决了。总之dealloc里面的[NSObject cancelPreviousPerformRequestsWithTarget:self]方法不一定有用,但是加上有益无害。

还是不太明白?请看下面代码:

@interface Person : NSObject
@end

@implementation Person
- (void)personTest{
    NSLog(@"personTest");
}

- (void)invoke{
    [self performSelector:@selector(personTest) withObject:nil afterDelay:0];
}
- (void)dealloc{
    [self invoke];
//    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}
@end

Person *person = [Person new];person实例释放的时候必然会奔溃。

测试工程

2.[NSObject performSelector]系列函数和
[NSRunLoop performSelector]系列函数区别

下面是官方API,都是为了实现延迟调用,但是你们知道他们的区别吗?

/****************   Delayed perform  ******************/

@interface NSObject (NSDelayedPerforming)

- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes;
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;

@end

@interface NSRunLoop (NSOrderedPerform)

- (void)performSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg order:(NSUInteger)order modes:(NSArray<NSRunLoopMode> *)modes;
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg;
- (void)cancelPerformSelectorsWithTarget:(id)target;

@end

下面是CocoaAsyncSocket早期基于runLoop实现的部分代码

代码一:
- (BOOL)moveToRunLoop:(NSRunLoop *)runLoop{
    NSAssert((theRunLoop == NULL) || (theRunLoop == CFRunLoopGetCurrent()),
             @"moveToRunLoop must be called from within the current RunLoop!");

    [runLoop performSelector:@selector(maybeDequeueRead) target:self argument:nil order:0 modes:theRunLoopModes];
    [runLoop performSelector:@selector(maybeDequeueWrite) target:self argument:nil order:0 modes:theRunLoopModes];
    [runLoop performSelector:@selector(maybeScheduleDisconnect) target:self argument:nil order:0 modes:theRunLoopModes];
}

代码二:
- (BOOL)setRunLoopModes:(NSArray *)runLoopModes{    
    NSAssert((theRunLoop == CFRunLoopGetCurrent()), @"setRunLoopModes must be called from within the current RunLoop!");
    [self performSelector:@selector(maybeDequeueRead) withObject:nil afterDelay:0 inModes:theRunLoopModes];
    [self performSelector:@selector(maybeDequeueWrite) withObject:nil afterDelay:0 inModes:theRunLoopModes];
    [self performSelector:@selector(maybeScheduleDisconnect) withObject:nil afterDelay:0 inModes:theRunLoopModes];
}

为了方便把下面方法定义为“方法1”

 [runLoop performSelector:@selector(maybeDequeueRead) target:self argument:nil order:0 modes:theRunLoopModes];

下面方法定义为“方法2”

  [self performSelector:@selector(maybeDequeueRead) withObject:nil afterDelay:0 inModes:theRunLoopModes];

"方法1","方法2"都干同样一件事,就是延迟执行maybeDequeueRead调用。但是它们是有区别的。区别如下:

NSRunLoop一般是和线程相关的。一个线程最多有一个NSRunLoop也可能没有。在这里可以将runLoop看做一个线程。
假设在“线程A”中调用moveToRunLoop,当线程A执行到moveToRunLoop方法中的[runLoop performSelector:@selector(maybeDequeueRead) target:self argument:nil order:0 modes:theRunLoopModes]这段代码的时候。“线程A”唤起另一个线程(runLoop所在的线程)假设为“线程B”去调用maybeDequeueRead方法。实现效果和下面方法一致*- (void)performSelector:(SEL)aSelector onThread:(NSThread )thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait (只是一个基于RunLoop实现,一个基于Thread调用)。这样就实现了多线程切换调用。
但是如果用下面方法[self performSelector:@selector(maybeDequeueRead) withObject:nil afterDelay:0 inModes:theRunLoopModes]则所有调用都在”线程A“中。

先整理到这吧,后面还会继续分析CocoaAsyncSocket学些心得。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    made_China阅读 1,236评论 0 7
  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    SOI阅读 21,905评论 3 63
  • Objective-C中有两个NSObject,一个是NSObject类,另一个是NSObject协议。而其中NS...
    ScaryMonsterLyn阅读 776评论 0 2
  • 父类实现深拷贝时,子类如何实现深度拷贝。父类没有实现深拷贝时,子类如何实现深度拷贝。• 深拷贝同浅拷贝的区别:浅拷...
    JonesCxy阅读 1,103评论 1 7
  • 露凝结秋风的寒聚集秋阳的暖露深藏着青草的情流露着花朵的爱露闪烁的是秋去冬来的泪
    xuxiaoyu4阅读 226评论 1 5