ios - 如何得到程序运行的线程,然后关闭它。(GCD创建的线程)
伊谢尔伦
伊谢尔伦 2017-04-17 17:36:48
0
1
237

如果某个ViewController里运行了一个线程,线程还没结束的时候,这个ViewController被pop了,
线程不结束,viewController会一直保留不会被释放,
这个时候再次进入这个viewController的时候会出现异常,
现在的问题是:如何得到这个线程,然后关闭它。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
大家讲道理

To put it simply, if it is a thread dispatched by gcd, it cannot be stopped once it starts running. If you must close the thread, there seem to be only two ways:

  1. Set a flag, check this flag repeatedly in the middle of the thread (for example, at the beginning of each loop), and return directly when it is time to close;

  2. You can use NSOperationQueue instead. NSOperationQueue

就你的需求而言,我不太明白为啥『再次进入这个viewController的时候会出现异常』,一般再次 push 进来的应该是一个新 new 出来的 ViewController,跟之前的不是同一个了,所以不应该有问题。难道你的 viewController 是单例吗?

可以试试在 block 里用到 viewController 都用 weakSelf __weak typeof(self) weakSelf = self;

🎜As far as your needs are concerned, I don't quite understand why "an exception occurs when entering this viewController again". Generally, what comes in when pushed again should be a new ViewController, which is not the same as the previous one, so no There should be a problem. Is your viewController a singleton? 🎜 🎜You can try using weakSelf __weak typeof(self) weakSelf = self; when using viewController in a block. In this way, the block will not hold the viewController, and it will naturally become nil when it pops out. . But if it is a singleton, there is no way. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template