欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IOS用GCD调接口

程序员文章站 2022-05-28 22:51:00
...

本文以UIActivityIndicatorView为例,有没有同学试过调接口,调接口的时候会不会想要有个UIActivityIndicatorView去转圈圈来表示正在等待呢?但是你如果直接调接口的话,即使你用UIActivityIndicatorView ,会不会怎么也无法显示呢? 如果遇到这种情况,用GCD就可以,不过好麻烦就是了…… 

 

- (IBAction)queryUser:(id)sender
{
    [telephoneTextField resignFirstResponder];
    
    //开始转圈圈
    [indicator setHidden:NO];
    [indicator setHidesWhenStopped:YES];
    [indicator startAnimating];//转圈圈
    //采用GCD,如果调接口成功或者失败,圈圈停止
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *telephone = telephoneTextField.text;
        if ([telephone length] == 0)
        {
            //执行主线程
            dispatch_async(dispatch_get_main_queue(),  ^{
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"业务号码不能为空" message:@"请重新输入条件查询!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
                
                [indicator stopAnimating];//停止圈圈
            });
            return;
        }
        //调用接口
        UserQueryBO *userQueryBO = [[UserQueryBO alloc] init];
        NSDictionary *resultList = [userQueryBO queryUser:telephone forUser:nil];
//接口返回值
        NSString *returnCode = [NSString stringWithFormat:@""];
        NSString *message= [NSString stringWithFormat:@""];
        NSString *resultInfo= [NSString stringWithFormat:@""];
        if (resultList != nil)
        {
            returnCode = [resultList objectForKey:@"ReturnCode"];
            message = [resultList objectForKey:@"Message"];
            resultInfo = [resultList objectForKey:@"ResultInfo"];
            //NSLog(@"%@", resultInfo);
        }
        if ([returnCode isEqualToString:@"0"])
        {
            if ([resultInfo length] > 0)
            {
                //成功调用主线程,利用主线程里navigationController去打开新页面
                dispatch_async(dispatch_get_main_queue(),  ^{
                    UserQueryShowVC *userQueryShowVC = [[UserQueryShowVC alloc] init];
                    userQueryShowVC.userInfo = resultInfo;
                    [self.navigationController pushViewController:userQueryShowVC animated:YES];
                    self.navigationController.title = @"用户查询详情";
                    [userQueryShowVC release];
                });
                
            }
            else
            {
                //接口调用失败,利用主线程打开输入框
                dispatch_async(dispatch_get_main_queue(),  ^{
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"没查询到相关用户信息" message:@"请重新输入条件查询!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                    [alertView release];
                    [indicator stopAnimating];//停止圈圈
                });
            }
        }
        else
        {
            //接口调用失败,利用主线程打开输入框
            dispatch_async(dispatch_get_main_queue(),  ^{
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"查询失败" message:[@"请重新输入条件查询!失败信息:" stringByAppendingString:message] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
                [indicator stopAnimating];//停止圈圈
            });
        }
        //[resultInfo release];
        //[message release];
        //[returnCode release];
        [userQueryBO release];
        //这里,保险起见,再停止圈圈……
        dispatch_async(dispatch_get_main_queue(),  ^{
            
            [indicator stopAnimating];
        });
    });
    
}

 代码如上,用//采用GCD

    dispatch_async(dispatch_get_global_queue(0, 0), ^{});将接口逻辑放在这里,如果需要停止圈圈或者弹出对话框,则用

dispatch_async(dispatch_get_main_queue(), ^{ 

            [indicator stopAnimating];

 

        });

去会写数据到主线程。

 

记得,不这样的话会报错的说