如何发布NSThread是否有任何特定的发布过程? [英] How to release a NSThread is there any particular process to release?

查看:72
本文介绍了如何发布NSThread是否有任何特定的发布过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的搜索控制器代码中,我正在使用NSthread。对于我们在搜索栏中输入的每个字符,在线程启动后立即创建一个新线程。我们如何释放这些导致内存泄漏的NSThread?

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?

这是我创建NSThreads的地方:

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];

}


推荐答案

答案: self.searchThread = nil; 将在执行后释放线程

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.

这篇关于如何发布NSThread是否有任何特定的发布过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆