多线程之dispatch_queue_t

时间:2021-03-16 23:32:54

gcd异步多线程操作使用流程,一般在子线程中处理数据,主线程更新界面

static dispatch_queue_t loadDealersQueue = NULL;
    if (!loadDealersQueue) {
        loadDealersQueue = dispatch_queue_create("com.geelycar.loadactivity.loaddealersqueue", NULL);
    }
    
    dispatch_async(loadDealersQueue, ^{
        @autoreleasepool {
            //Data processing
            dispatch_async(dispatch_get_main_queue(), ^{
            //Update Interface
            });
        }
    
    });

今天又张见识了,看下如下的子线程与主线程的调用

 static dispatch_queue_t searchQueue = NULL;
    if( !searchQueue )
        searchQueue = dispatch_queue_create("com.moneytree.searchqueue", NULL);//调度队列
    
//异步运行 dispatch_async(searchQueue,
^{ @autoreleasepool {//ARC模式下自动释放池的写法
//数据处理放在队列中 NSArray
*resultData = [[NSArray alloc] initWithArray:[[MTStockManage sharedInstance] stockListMatchingSearchText:searchText fromArray:self.stockList]]; self.stockList = resultData; [self setTableDataSource:nil]; if (resultData.count > SHOW_STOCK_NUMBER) { NSRange range = NSMakeRange(0, SHOW_STOCK_NUMBER); [self setTableDataSource:[resultData subarrayWithRange:range]]; }else { [self setTableDataSource:resultData]; } dispatch_async(dispatch_get_main_queue(), ^{//返回主界面 [self.dataResultTable reloadData]; //主线程中刷新UI }); } });