一个方法只使用一个NSThread

时间:2012-04-28 06:43:27

标签: objective-c multithreading core-data nsthread

我的应用程序中有刷新按钮,后台线程中运行方法“doRefresh”。现在,如果用户按下双击 - 方法doRefresh运行2次以上。 我想如果用户按下双击和更多点击,所有previos线程都会停止工作并完成。并且新方法开始在新线程中运行。我该怎么做?

更多细节:我使用CoreData,并解析填充关系对象的长XML文件。例如,当用户按下刷新按钮10次时,请抓住下一个错误:

 reason: '*** Collection <__NSCFSet: 0x5b7dd50> was mutated while being enumerated.<CFBasicHash 0x5b7dd50 [0x1503400]>{type = mutable set, count = 8   

1 个答案:

答案 0 :(得分:1)

Here's how things may work
1. Keep a reference to last thread spawned
2. In each thread spawned keep a variable, say var_cancel
3. When a new thread is created, set the var_cancel to true for previous thread.
4. In the thread, check for var_cancel at short durations. If found true, exit the thread.
5. Make sure the thread does not produce any side effects between var_cancel set true to exit of thread.