InjoyDeng / ResignTool

An app for macOS that can (re)sign apps and bundle them into ipa files that are ready to be installed on an iOS device. Unrestricted by the applicationIdentifier in the ProvisioningProfile file.
MIT License
407 stars 111 forks source link

关于工程中的IDRunloop中的保活机制的疑问 #2

Closed yangfangkuo closed 5 years ago

yangfangkuo commented 5 years ago

你好,打扰一下,有一点好奇,在楼主的代码中,有这么一段代码

- (instancetype)init {
    if (self = [super init]) {
        self.isSuspend = NO;
    }
    return self;
}

-(void)run:(void (^)(void))block {
    self.isSuspend = NO;
    dispatch_async(dispatch_get_global_queue(DISPATCH_TIME_NOW, 0), ^{
        while (!self.isSuspend) {
            block();
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }
    });
}

- (void)stop:(void (^)(void))complete {
    self.isSuspend = YES;

    dispatch_async(dispatch_get_main_queue(), ^{
        complete();
    });
}

其中Runloop中的gcd开辟的子线程,在做DoWhile循环,那么在DoWhile循环那里面,为什么还要获取RunLoop呢,当前类的作用不是就是为了类似实现KVO的功能吗,请作者给指点迷津,谢谢

InjoyDeng commented 5 years ago

保证 block() 中的异步操作能完整执行

yangfangkuo commented 5 years ago

但是我感觉不加也能执行吧,一做doWhile循环如果不打破循环,线程会停吗?谢谢

发自我的 iPhone

在 2018年12月1日,下午6:31,Injoy notifications@github.com 写道:

保证 block() 中的异步操作能完整执行

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

InjoyDeng commented 5 years ago

这里的 while 的作用并不是重复执行 block ()。当执行到 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];将会阻塞线程,而 block ()不会受到影响,直到 isSuspend == YES才会继续执行之后的代码。也就是说不论什么情况, block()始终只会执行一次。

所以这个类的作用是执行异步方法并等待返回结果,当收到结果后继续执行之后的任务。

这段代码的效果确实比较有意思,建议敲出来自己试试。

InjoyDeng commented 5 years ago

上面的回答有修改,如果邮件没有提示,需要上 GitHub 才能看到吧。

yangfangkuo commented 5 years ago

其实刚开始的确是这样理解的,但是我把这个类放到新建的工程里面去试了一下,我在while里面加了打印,本来是别人也是和你这样解释的一样,但是实践发现,无论是否获取runloop,while内的打印一直在打印,不知道是否和你的期望一致,我其实也刚开始理解的是以为是仿的系统runloop的休眠呢,但是实践之后,发现好像不是,或者这个while内的打印一直打印的原因是其他原因?

发自我的 iPhone

在 2018年12月3日,上午1:06,Injoy notifications@github.com 写道:

这里的 while 的作用并不是重复执行 block ()。当执行到 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];将会柱塞线程,直到 isSuspend == YES才会继续执行之后的代码。

所以这里 while + RunLoop 的作用是等待异步的 block ()返回结果,当收到结果后继续执行之后的任务。

这段代码的效果确实比较有意思,建议敲出来自己试试。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

yangfangkuo commented 5 years ago

这个是我实践的代码 不知道使用NSLog有什么影响吗 image image