AsyncSocket 源码分析

作者: upworld | 来源:发表于2017-01-06 00:15 被阅读291次

    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学些心得。

    相关文章

      网友评论

      本文标题:AsyncSocket 源码分析

      本文链接:https://www.haomeiwen.com/subject/smrtbttx.html