예전꺼(2014년이전꺼)/iOS
NSThread
리베스
2011. 6. 15. 11:00
주요 method
-(BOOL)isExecuting; - Thread가 실행중인지
-(BOOL)isFinished; - Thread가 끝났는지
-(BOOL)isCancelled; - Thread사용중 취소 됐는지
-(void)cancel; - Thread 사용종료
-(void)start; - Thread 사용시작
cancel을 사용한다고 해서 Thread가 바로 중지 되지는 않으며 isCancelled 상태를 YES로만 만들어 준다.
Thread를 실행하고 있는 함수내부 에서 isCancelled를 체크 해서 종료를 유도하는게 낳을듣 싶다.
헤더파일에 NSThread *th; 가 선언 되었다고 가정
-(void) tempMethod {
th = [[NSThread alloc] initWithTarget:self selector:@selector(thMethod)
object:nil];
[th start];
}
-(void) thMethod {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while ([[NSThread currentThread] isCancelled] == NO) {
/* 처리 루틴 */
}
-(void) thStop {
[th cancel]; // -->이때 isCancelled 가 YES가 됨
}
머 짜바랑 거의 똑같내
-그 밖에
[self performSelectorOnMainThread:@selector(tempMethod:) withObject:result waitUntilDone:NO];
호출된 함수는 mainThread에서 처리 된다.
-> UI와 관련된건 무조건 Main Thread에서 처리 해줘야 한다. - 확실한지는 모름 (이런 무책임함...-_-)