1

데이터를 프리 페치하고 uitableview에 표시하는 데 문제가 있습니다. 그래서 기본적으로 웹에서 데이터를 가져올 수 있도록 기본 UI 스레드를 차단하고 싶습니다. 동기화를 위해 직렬 디스패치 대기열을 사용하고 있습니다. 또한 디스패치 대기열 블록은 웹에서 데이터를 가져 오는 다른 블록을 실행 중입니다. 실행 코드는 viewdidload로 작성됩니다.디스패치 대기열을 사용하여 주 스레드를 동기화하는 것과 관련된 문제가 발생했습니다.

dispatch_queue_t queue= dispatch_queue_create("myQueue", NULL); 


CMStore *store = [CMStore defaultStore]; 

// Begin to fetch all of the items 
dispatch_async(queue, ^{ 

[store allObjectsOfClass:[Inventory class] 
     additionalOptions:nil 
       callback:^(CMObjectFetchResponse *response) { 

        //block execution to fetch data 

       }]; 
}); 
dispatch_async(queue, ^{ 
//load data on local data structure 


    [self.tableView reloadData]; 
}); 
+0

주 스레드 차단은 강력히 권장하지 않습니다. 웹에서 배경 스레드 (현재 수행중인 것처럼 보임)에서 데이터를로드하고 [tableView reloadData]를 호출 할 때 dispatch_get_main_queue로 디스패치하여 기본 대기열에 있는지 확인하십시오. – geraldWilliam

답변

8

주 스레드/대기열 이외의 다른 곳에서는 UI 관련 코드를 수행하지 않아야합니다.

메인 스레드/대기열의 모든 UI 관련 코드 (은 UITableView)를 항상 수행하십시오. 귀하의 예제에서 데이터를 가져온 경우에만 그래서 콜백이 호출되기 전에 완료 블록에서 테이블 뷰를 다시로드해야합니다 같아요.

// Begin to fetch all of the items 
dispatch_async(queue, ^{ 
    [store allObjectsOfClass:[Inventory class] 
     additionalOptions:nil 
       callback:^(CMObjectFetchResponse *response) { 

       // block execution to fetch data 
       ... 
       // load data on local data structure 
       ... 

       // Ask the main queue to reload the tableView 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        // Alsways perform such code on the main queue/thread 
        [self.tableView reloadData]; 
       }); 
    }]; 
}); 
+0

@ AliSoftware- 감사합니다. 충고. 나는 그런 문제를 다음 번에 생각할 때 염두에 두겠다 .. !! – pbd

관련 문제