iOS新手学习

iOS-GCD+RAC(信号量,用于多个请求)

2019-03-06  本文已影响0人  032c6843a285

需求:开发过程中某个界面多个请求,且有顺序~ 推荐2个方法,直接上代码块(即复制即用)

一、gcd 队列组
- (void)text_gcd{
    
    weakSelf(self);
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    
    // 主线程 顺序执行。dispatch_sync
    // 异步线程 顺序打乱 dispatch_async
    
    dispatch_group_enter(group);
    dispatch_sync(queue, ^{
        SWLog(@"开始请求1111111");
        [weakSelf get_TaskData:^{
            SWLog(@"请求完成 11111111");
            dispatch_group_leave(group);
        }];
    });

    dispatch_group_enter(group);
    dispatch_sync(queue, ^{
         SWLog(@"开始请求2222222");
        [weakSelf get_TaskData:^{
            SWLog(@"请求完成 222222222");
            dispatch_group_leave(group);
        }];
    });

    dispatch_group_enter(group);
    dispatch_sync(queue, ^{
        SWLog(@"开始请求33333");
        [weakSelf get_TaskData:^{
            SWLog(@"请求完成 3333333");
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        SWLog(@"请求上面123 后走这里,回到主线程 刷新UI");
    });
    
    
}
#pragma mark - request
//模拟耗时请求
- (void)get_TaskData:(void(^)())successBlock{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        successBlock();
    });
}
  • 打印结果
2019-03-06 16:22:30.365326+0800 MVVM+RAC[7201:533505] < ViewController.m:(109) > 开始请求1111111
2019-03-06 16:22:30.365474+0800 MVVM+RAC[7201:533505] < ViewController.m:(121) > 开始请求2222222
2019-03-06 16:22:30.365560+0800 MVVM+RAC[7201:533505] < ViewController.m:(130) > 开始请求33333
2019-03-06 16:22:30.463713+0800 MVVM+RAC[7201:533710] [] nw_proxy_resolver_create_parsed_array PAC evaluation error: NSURLErrorDomain: -1004
2019-03-06 16:22:32.447209+0800 MVVM+RAC[7201:533505] < ViewController.m:(111) > 请求完成 11111111
2019-03-06 16:22:32.447537+0800 MVVM+RAC[7201:533505] < ViewController.m:(123) > 请求完成 222222222
2019-03-06 16:22:32.447944+0800 MVVM+RAC[7201:533505] < ViewController.m:(132) > 请求完成 3333333
2019-03-06 16:22:32.448110+0800 MVVM+RAC[7201:533505] < ViewController.m:(138) > 请求上面123 后走这里,回到主线程 刷新UI


二、RAC(ReactiveCocoa(其简称为RAC)是由GitHub开源的一个应用于iOS和OS X开发的新框架。RAC具有函数式编程响应式编程的特性)
pod 'ReactiveObjC', '~> 3.0.0' //建议纯oc代码使用ReactiveObjC

//信号量请求 类似gcd
- (void)liftSelector{
    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        SWLog(@"请求111");
        [subscriber sendNext:@"11111成功"];
        return nil;
    }];
    
    RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
         SWLog(@"请求2222");
        [subscriber sendNext:@"222222成功"];
        return nil;
    }];
    
    [self rac_liftSelector:@selector(refeshUI::) withSignals:signal1,signal2, nil];
    
}
-(void)refeshUI:(id)one :(id)two{
    NSLog(@"%@---%@",one,two);
}


Log 输出
2019-03-07 14:14:29.163017+0800 MVVM+RAC[9845:694036] < ViewController.m:(115) > 请求111
2019-03-07 14:14:32.189107+0800 MVVM+RAC[9845:694036] < ViewController.m:(121) > 请求2222
2019-03-07 14:14:34.192020+0800 MVVM+RAC[9845:694036] 11111成功---222222成功

怎么样,RAC好用吧~,我会另外写一篇文章专门简单学习RAC的

如果对你有帮助, 随手一❤️,如果写的不对的地方,麻烦指出~哈哈,感谢
上一篇 下一篇

猜你喜欢

热点阅读