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:
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;
You can use NSOperationQueue instead. NSOperationQueue。
就你的需求而言,我不太明白为啥『再次进入这个viewController的时候会出现异常』,一般再次 push 进来的应该是一个新 new 出来的 ViewController,跟之前的不是同一个了,所以不应该有问题。难道你的 viewController 是单例吗?
🎜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. 🎜
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:
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;
You can use
NSOperationQueue
instead.NSOperationQueue
。就你的需求而言,我不太明白为啥『再次进入这个viewController的时候会出现异常』,一般再次 push 进来的应该是一个新 new 出来的 ViewController,跟之前的不是同一个了,所以不应该有问题。难道你的 viewController 是单例吗?
可以试试在 block 里用到 viewController 都用 weakSelf
🎜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;
__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. 🎜