在項(xiàng)目中往往會(huì)遇到這種需求:UI 的更新要在2~3個(gè)網(wǎng)絡(luò)請(qǐng)求后才執(zhí)行.這里提供兩種執(zhí)行方案.
一.利用 GCD
通過 gcd_group可以解決這個(gè)問題.具體做法如下
//
// ViewController.m
// tableview
//
// Created by myMac on 16/12/26.
// Copyright ? 2016年 myMac. All rights reserved.
//
import "ViewController.h"
typedef void(^FinishNetwork)();
@interface ViewController ()
@property (nonatomic, copy ) FinishNetwork block;
@property (nonatomic, copy ) NSString *string1;
@property (nonatomic, copy ) NSString *string2;
@property (nonatomic, copy ) NSString *string3;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initData];
}
-
(void)initData {
// 創(chuàng)建信號(hào)量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// 創(chuàng)建全局并行
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{// 請(qǐng)求一 //這里通過 block 表示請(qǐng)求結(jié)束,并標(biāo)記一個(gè)信號(hào)量 [self getList1:^{ dispatch_semaphore_signal(semaphore); }];
});
dispatch_group_async(group, queue, ^{// 請(qǐng)求二 [self getList2:^{ dispatch_semaphore_signal(semaphore); }];
});
dispatch_group_async(group, queue, ^{// 請(qǐng)求三 [self getList3:^{ dispatch_semaphore_signal(semaphore); }];
});
dispatch_group_notify(group, queue, ^{
//在這里 進(jìn)行請(qǐng)求后的方法 NSLog(@"string1:___%@", _string1); NSLog(@"string2:___%@", _string2); NSLog(@"string3:___%@", _string3); // 三個(gè)請(qǐng)求對(duì)應(yīng)三次信號(hào)等待 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
}
-
(void)getList1:(FinishNetwork)block {
NSLog(@"加載列表1");
self.string1 = @"加載列表1";
}
-
(void)getList2:(FinishNetwork)block {
NSLog(@"加載列表2");
self.string2 = @"加載列表2";
}
-
(void)getList3:(FinishNetwork)block {
NSLog(@"加載列表3");
self.string3 = @"加載列表3";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
二.通過 RAC
利用 RAC 的 merge 也可以解決這個(gè)問題,具體做法如下
ViewController
[[RACSignal merge:@[[TestViewModel fetchList1], [TestViewModel fetchList2]]] subscribeNext:^(id x) {
NSLog(@"%@", x);
if (!_string1.length) {
self.string1 = x;
} else {
self.string2 = x;
}
} completed:^{
NSLog(@"string1: %@\nstring2: %@ \n", _string1, _string2);
}];
ViewModel
import "TestViewModel.h"
@implementation TestViewModel
-
(RACSignal *)fetchList1 {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"請(qǐng)求1"]; [subscriber sendCompleted]; return nil;
}];
}
-
(RACSignal *)fetchList2 {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"請(qǐng)求2"]; [subscriber sendCompleted]; return nil;
}];
}
@end
另外,通過下面的方法也可以實(shí)現(xiàn)
<span style="font-size:18px;">/// Like -rac_liftSelector:withSignals:, but accepts an array instead of
/// a variadic list of arguments.
- (RACSignal *)rac_liftSelector:(SEL)selector withSignalsFromArray:(NSArray *)signals;</span>
具體實(shí)現(xiàn)代碼
[self rac_liftSelector:@selector(responseA:B:) withSignalsFromArray:@[[TestViewModel fetchList1], [TestViewModel fetchList2]]];
- (void)responseA:(id)a B:(id)b {
NSLog(@"%@, %@", a, b);
}
打印出的結(jié)果