2012-04-28 4 views
0

내 앱에서 새로 고침 버튼이 있고 백그라운드 스레드에서 실행되는 'doRefresh'메서드가 있습니다. 이제 사용자가 두 번 클릭하면 - 방법 doRefresh이 2 번 이상 실행됩니다. 사용자가 두 번 클릭하고 더 많은 클릭을하면 모든 previos 스레드가 작업을 중단하고 완료합니다. 그리고 새로운 방법은 새로운 스레드에서 시작됩니다. 어떻게해야합니까?한 가지 방법으로 하나의 NSThread 만 사용

추가 세부 정보 : CoreData를 사용하여 채우기 관계 개체에 대한 긴 XML 파일을 구문 분석합니다. 새로 고침 버튼 예를 들어 10 배에 사용자 프레스 안녕하세요, 다음 오류를 잡을 때 :

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

답변

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. 
+0

나는 백그라운드 스레드 (performSelectorToBackground)이 할 수 있습니까? –

+0

직접 시도하지는 않았지만 메서드에 전달 된 개체에서 cancel 변수를 사용하고 전달 된 마지막 개체의 참조를 유지해야합니다. –

관련 문제