투케이2K

136. (Objective-C/objc) DispatchGroup 사용해 작업 그룹 묶음 및 순차 로직 처리 실시 (waitGroup.notify) - DispatchQueue 본문

Objective-C

136. (Objective-C/objc) DispatchGroup 사용해 작업 그룹 묶음 및 순차 로직 처리 실시 (waitGroup.notify) - DispatchQueue

투케이2K 2023. 11. 1. 08:52
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

// ------------------------------------------------------------------------------
// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
// ------------------------------------------------------------------------------
- (void)testMain {
    [S_Log _D_WithC_:[NSString stringWithFormat:@"%s", __FILE__]
            M_:[NSString stringWithFormat:@"%s :: %d", __FUNCTION__, __LINE__]
            description:@"테스트 함수 시작 실시" data:nil];

    /*
     ------------------------------------
     [요약 설명]
     ------------------------------------
     1. Dispatch Group : DispatchQueue 들을 그룹으로 묶어서, 후행 클로저 (일이 끝난고 난 다음의 로직 처리) 를 할 때 사용 됩니다
     ------------------------------------
     2. enter() : 선행 시작을 알립니다
     ------------------------------------
     3. leave() : 실행이 끝난 다는 것을 알립니다
     ------------------------------------
     4. notify() : 그룹 작업이 완료 되었음을 알립니다
     ------------------------------------
     */

    
    
    // [try catch 구문 정의 실시]
    @try {
        
        // [로직 처리 실시]
        __block int one = 0;
        __block int two = 0;
        
        
        dispatch_queue_t jobQueue = dispatch_get_main_queue(); // [작업 큐]
        dispatch_group_t waitGroup = dispatch_group_create(); // [그룹]
        
        
        dispatch_group_enter(waitGroup); // [선행 시작 알림]
        dispatch_async(jobQueue, ^{
            one = 1; // 변수 값 삽입
                        
            [NSThread sleepForTimeInterval:1.0f]; // 1 초 간 정지

            dispatch_group_leave(waitGroup); // 실행 종료
        });
        
        
        dispatch_group_enter(waitGroup); // [선행 시작 알림]
        dispatch_async(jobQueue, ^{
            two = 2; // 변수 값 삽입
                        
            [NSThread sleepForTimeInterval:1.0f]; // 1 초 간 정지

            dispatch_group_leave(waitGroup); // 실행 종료
        });
        
        
        dispatch_group_notify(waitGroup, dispatch_get_main_queue(), ^{
            
            [S_Log _D_WithC_:[NSString stringWithFormat:@"%s", __FILE__]
                    M_:[NSString stringWithFormat:@"%s :: %d", __FUNCTION__, __LINE__]
                    description:@"그룹 작업 실행 종료" 
                        data:[NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"one :: %d", one],
                              [NSString stringWithFormat:@"two :: %d", two],
                              nil]];
            
        });

    }
    @catch (NSException *exception) {
        NSLog(@"\n[NSException : 예외 상황 발생] : %s\n", exception.description.UTF8String);
    }
}
 

[결과 출력]

 

================================================================
LOG :: CLASS PLACE :: A_Intro.m :: -[A_Intro testMain]_block_invoke_2 :: 299
-------------------------------------------------
LOG :: NOW TIME :: 2023-11-01 08:49:50
-------------------------------------------------
LOG :: DESCRIPTION :: 그룹 작업 실행 종료
-------------------------------------------------
LOG :: one :: 1
-------------------------------------------------
LOG :: two :: 2
================================================================

 

반응형
Comments