모든 초기 비동기 인증 단계가 성공하려면 여러 비동기 작업이 있습니다. 세마포어를 사용하여 인증이 완료 될 때까지 모든 보안 작업을 차단합니다. 작업은 인증이 끝날 때 획득 한 보안 토큰에 의존하기 때문에 주로 타이밍 목적으로 사용됩니다. 인증에는 네트워크 요청이 포함되며 몇 초가 걸릴 수 있습니다.많은 수를 차단하기 위해 세마포를 사용하여 모두 해제
내 코드의 난이도는 인증 후 발행 된 dispatch_semaphore_signal()
이 첫 번째 세마포어 잠금을 계속할 수 있다는 신호 만 보내는 것으로 보입니다. 두 번째는 계속 차단 될 것입니다. 앞으로도 많은 모의 차단 작업이있을 수 있으며, 모두 세마포어를 기다리고 있습니다.
이 차단에 대해 명확한 방법이 있는지 궁금합니다. 각 대기중인 작업이 즉시 dispatch_semaphore_signal()
을 발행하여 다음 작업을 릴리스 할 수 있다고 생각합니다. 한 번의 호출로 모든 차단 세마포를 해제 할 수 있습니까?
GCD로 더 깨끗한 방법이 있습니까? 나는 GCD에 능숙하지 못하다. 그래서 코드 스 니펫 (code snippets)이 아래의 사용법에서 도움이된다.
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
// in actuality, these 3 may be any where in the app, in different classes, methods, etc
// so a completionHandler is not possible
[self authentication]; // async, could take many seconds
[self authenticatedTask1]; // async
[self authenticatedTask2]; // async
- (void) authentication {
// async url request, assume it is configured here
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// authenticate
authenticated = TRUE;
secure_token = @"4rjiofwefsdf"; // obtained during auth
dispatch_semaphore_signal(sem);
}];
}
- (void) authenticatedTask1 {
// put on new thread, so semaphore doesn't block program
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
if(!authenticated){
// wait until authenticated
dispatch_semaphore_wait(sem)
}
// continue after authenticated, using secure_token
});
}
- (void) authenticatedTask2 {
// put on new thread, so semaphore doesn't block program
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
if(!authenticated){
// wait until authenticated
dispatch_semaphore_wait(sem)
}
// continue after authenticated, using secure_token
});
}
어떻게 수행 할 수 있는지 설명 할 수있는 코드 스 니펫을 제공 할 수 있습니까? – Miro