how to stop nsthread

前端 未结 4 1733
悲哀的现实
悲哀的现实 2021-01-06 18:57

I am using a thread to update messages in background in my application. The thread is started in my messages class.

Messages.m

ti         


        
4条回答
  •  鱼传尺愫
    2021-01-06 19:22

    [timerThread cancel] doesn't stop thread, it just marks it as cancelled. I presume that your startTimerThread: performs some sort of infinite loop. You have to check isCancelled in this loop periodically and if it's YES you should perform some clean up and break from this loop.

    BTW if you don't perform updates frequently it is more convenient to run NSTimer in main thread and detach new thread on callbacks (like [self performSelectorInBackground:@selector(updateMessages:) withObject:whatever]) or start an instance of NSOperation.

提交回复
热议问题