让Block顺序执行

来源:互联网 发布:知柏地黄丸功效与禁忌 编辑:程序博客网 时间:2024/06/07 06:30

利用GCD的 dispatch_semaphore_t 设置线程依赖 



+ (NSString *)getErrorTextWithCustmer:(KPCustomer *)customer

{

__block NSString *alertStr;

        //先创建一个semaphore

//        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);

        

        KPViewRequest *request = [KPViewRequest message];

        request.minCtime = [[DataManager getInstance].localManager getSyncTime];

        

        

        KPViewRequest_Option *viewRequest_Option = [KPViewRequest_Option message];

        viewRequest_Option.type = KPViewFinanceType_FinanceCustomer;

        viewRequest_Option.value = [NSString stringWithFormat:@"%lld",customer.customerId];

        [request.optionArray addObject:viewRequest_Option];

        

        [[DataManager getInstance].financeManager reloadData];

        [[DataManager getInstance].financeManager reloadFinance:request];

        [[DataManager getInstance].financeManager reloadFinancePage:^(FinancePagination * _Nullable pagination) {

            if (pagination) {

                for (KPFinance *message in pagination.list) {

                    if (message.local) {

                        //发出已完成的信号

                        alertStr = ML(@"部分收款纪录未上传,线上数据可能存在差异");

                        dispatch_semaphore_signal(semaphore);

                        break;

                    }

                }

                //发出已完成的信号

                dispatch_semaphore_signal(semaphore);

            }

        }];

        

        //等待执行,不会占用资源

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        

return alertStr;

}


原创粉丝点击