liebus

NSThread 본문

예전꺼(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) {

        /* 처리 루틴 */

     } 


     [pool release];  


-(void) thStop {
     [th cancel];   // -->이때 isCancelled 가 YES가 됨     
}

머 짜바랑 거의 똑같내


-그 밖에 

[self performSelectorOnMainThread:@selector(tempMethod:) withObject:result waitUntilDone:NO]; 

 

 
performSelectorOnMainThread 를 사용하여 메소드를 호출 하면 (위의 tempMethod) 
                                                                        호출된 함수는 mainThread에서 처리 된다.  
 -> UI와 관련된건 무조건 Main Thread에서 처리 해줘야 한다. - 확실한지는 모름 (이런 무책임함...-_-)


 

'예전꺼(2014년이전꺼) > iOS' 카테고리의 다른 글

애플 Certificate등록, Devices, App ID, Provisioning 등록 과정  (0) 2011.08.03
cocos2d 설정  (0) 2011.06.18
sleep과 Delay  (0) 2011.05.23
Tag로 view가저 오기  (0) 2011.05.23
Documents Path  (0) 2011.05.04
Comments