TechLog
performSelector를 사용해 서로 다른 runLoop에서 코드 수행하기
Kenial
2011. 5. 3. 14:09
특정 버튼을 탭했을 때 호출되는 다음과 같은 액션 메서드가 있다고 가정해 보자 :
- (IBAction)buttonTouched:(NSObject *)sender { performindex = 0; [self performSelector:@selector(testMethod1) withObject:nil afterDelay:0.2f]; [self performSelectorInBackground:@selector(testMethod2) withObject:nil]; NSLog(@"buttonTouched out, runloop : %x", [NSRunLoop currentRunLoop]); } - (void)testMethod1 { NSLog(@"testMethod1 in, runloop : %x", [NSRunLoop currentRunLoop]); [NSThread sleepForTimeInterval:0.1f]; NSLog(@"testMethod1 out"); } - (void)testMethod2 { NSLog(@"testMethod2 in, runloop : %x", [NSRunLoop currentRunLoop]); [NSThread sleepForTimeInterval:0.1f]; NSLog(@"testMethod2 out"); }
위 코드는 다음과 같은 결과를 보여준다 :
2011-04-30 17:16:48.458 Test[2741:207] buttonTouched out, runloop : 4b36a80
2011-04-30 17:16:48.458 Test[2741:6a03] testMethod2 in, runloop : 4b398d0
2011-04-30 17:16:48.564 Test[2741:6a03] testMethod2 out
2011-04-30 17:16:48.658 Test[2741:207] testMethod1 in, runloop : 4b36a80
2011-04-30 17:16:48.759 Test[2741:207] testMethod1 out
특정 메서드를 UI 런루프 외의 다른 런루프(다시 말하면 별도의 백그라운드 스레드)에서 실행하고 싶다면, 위와 같이 간단하게 performSelectorInBackground를 사용하는 방법을 생각해 볼 수 있다.