How to stop nsthread

I am using a thread to update messages in the background in my application. The thread starts in my message class.

Messages .m

timerThread = [[NSThread alloc] initWithTarget:self
                selector:@selector(startTimerThread:) object:nil]; 
[timerThread start];

Now I want the thread to stop when the user writes out the application. For this, in the signout method (in another class), I added

Messages *msg=[[Messages alloc]init];  
[msg.timerThread cancel];  
[msg release];  

But even after exiting the application, the thread still works.

+3
source share
4 answers

(I assume that you are creating an instance of messages somewhere else in the program)

( ), (-cancel - - , ), (?). - , .

, , , , , - dealloc , .

+2

[timerThread cancel] , . , startTimerThread: - . isCancelled , , .

, , NSTimer (, [self performSelectorInBackground:@selector(updateMessages:) withObject:whatever]) NSOperation.

+4
[NSThread exit];

You have tested this method of the NSThread class.

+1
source

Use [NSThread exit];or[NSThread cancel];

0
source

Source: https://habr.com/ru/post/1796269/


All Articles