How to release a NSThread is there any particular process to release?

谁都会走 提交于 2020-01-06 20:54:23

问题


In my code for search controller I am using NSthread. For every character we enter in the search bar a new thread is created very next the thread is started. How do we release these NSThreads which are causing memory leaks?

Here is where my NSThreads are created:

if (searchString.length > 1) {
    self.searchThread = [[[NSThread alloc] initWithTarget:self selector:@selector(filterContentForSearchTextInBackgroundThread:) object:searchString] autorelease];
    [self.searchThread start];

}

回答1:


Answer: self.searchThread = nil; will release the thread when it is done executing.

Longer answer; you really really don't want to spawn a thread on every keystroke. If you are going to use threads at all, you should have ONE thread that processes keystrokes and you should tell that thread about the keystrokes as they happen.

Which means you are probably going to need some kind of "cancel the current query and start a new one" mechanism, too.



来源:https://stackoverflow.com/questions/6035177/how-to-release-a-nsthread-is-there-any-particular-process-to-release

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!