How to cancel/exit/stop execution of Thread object or thread running in background in IOS

后端 未结 3 1945
有刺的猬
有刺的猬 2020-12-10 09:50

I detaching The thread as given below to do some operation in background

 currentThread = [[NSThread   alloc]initWithTarget:contactServiceselector:@selector(         


        
3条回答
  •  执笔经年
    2020-12-10 10:23

    Your background thread needs to check to see if it has been cancelled, either through the isCancelled method...

    if ([[NSThread currentThread] isCancelled]) {
        // do cleanup here
        [NSThread exit];
    }
    

    You can't kill the thread externally because there is no way to know what state the thread might be in and, thus, killing it would produce indeterminate behavior (imagine if the thread was holding a mutex down in the allocator when it was killed... ouch).

提交回复
热议问题