串行发包的技术方案:
(1)条件锁。
(2)信号量。
//
// HsSplashPage.m
// CjhPhoneDemo(TzyjApp)
//
// Created by ChengJh on 2014-11-07.
// Copyright © 2019年 CompanyName. All rights reserved.
//
#import "HsSplashPage.h"
#import "HsAlertToastWidget.h"
@interface HsSplashPage ()
//@property(nonatomic,strong) NSCondition *condition;//iOS的条件锁。
//@property(nonatomic,assign) NSTimeInterval conditionTimeOut;//等待超时的时长(一般可设置为1(s)).
@property(nonatomic,strong) dispatch_semaphore_t semaphore;
@property(nonatomic,assign) BOOL requesting;
@end
@implementation HsSplashPage
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = HsColorWithHexStr(@"#F3F3F4");
//Initialize default value.
//self.condition = [[NSCondition alloc] init];
//self.conditionTimeOut = 1.0f;
self.semaphore = dispatch_semaphore_create(1);
UILabel *hintLbl = [[UILabel alloc] initWithFrame:CGRectMake(12, 60, 280, 21)];
hintLbl.font = HsFontRegular(16);
hintLbl.textColor = HsColorWithHexStr(@"#333333");
hintLbl.text = [NSString stringWithFormat:@"这里是%@页面。", self.class];
[self addSubview:hintLbl];
UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
refreshBtn.frame = CGRectMake(12, 120, 100, 40);
refreshBtn.layer.cornerRadius = 2.0;
refreshBtn.layer.borderColor = HsColorWithHexStr(@"#999999").CGColor;
refreshBtn.layer.borderWidth = 1.0;
[refreshBtn setTitleColor:HsColorWithHexStr(@"#666666") forState:UIControlStateNormal];
[refreshBtn setTitle:@"刷新" forState:UIControlStateNormal];
[refreshBtn addTarget:self action:@selector(clickedRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:refreshBtn];
//HsWeakSelf;
//_tblView.actionForDropDown = ^(BxkjTableView *sender) {
// if (YES == weakSelf.refreshingReq) {
// return;
// }
// weakSelf.requesting = YES;
// [weakSelf startRequestWithFinish:^(BOOL succ){
// weakSelf.requesting = NO;
// [weakSelf.tblView finishReqWithIsSucc:succ];
// }];
//};
}
return self;
}
- (void)didMoveToWindow {
[super didMoveToWindow];
if (self.window != nil) {
if (NO == _requesting) {
_requesting = YES;
CjhWeakSelf;
[self startRequestWithFinish:^(BOOL succ) {
weakSelf.requesting = NO;
}];
}
}
}
#pragma mark - Setter_/_Getter
#pragma mark - Method
- (void)clickedRefreshBtn:(UIButton *)sender {
if (YES == _requesting) {
[HsAlertToastWidget displayNotification:@"请稍候重试。"];
return;
}
_requesting = YES;
[HsAlertToastWidget displayNotification:@"开始刷新。"];
CjhWeakSelf;
[self startRequestWithFinish:^(BOOL succ) {
[HsAlertToastWidget displayNotification:@"刷新完成。"];
weakSelf.requesting = NO;
}];
}
- (void)startRequestWithFinish:(void(^)(BOOL succ))finish {
NSThread *trd = [[NSThread alloc]initWithTarget:self selector:@selector(threadActionWithFinish:) object:finish];
[trd start];
}
- (void)threadActionWithFinish:(void(^)(BOOL succ))finish {
@autoreleasepool {
CjhDispatchMainAsyncSafe(^{
//UIView *parentView = [UIApplication sharedApplication].delegate.window;
//[YJProgressHUD showProgress:@"加载中..." inView:parentView];
//[[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:6.0];//最多6.0s后自动消失。
});
//[_condition lock];//加锁。
//CjhLog(@"%s --- --- 0", __func__);
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
CjhLog(@"%s --- --- 1", __func__);
[self requestUpdateParam];
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
CjhLog(@"%s --- --- 2", __func__);
[self requestBasicData];
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
CjhLog(@"%s --- --- 3", __func__);
[self requestServerTime];
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
CjhLog(@"%s --- --- 4", __func__);
[self requestDownloadAdImgData];
//Insert code here.
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
CjhLog(@"%s --- --- N", __func__);
dispatch_semaphore_signal(_semaphore);
//[_condition unlock];//解锁。
CjhDispatchMainAsyncSafe(^{
//[YJProgressHUD hide];
});
dispatch_async(dispatch_get_main_queue(), ^{
if (finish != NULL) {
finish(YES);
}
});
}
}
- (void)requestUpdateParam {
NSString *httpUrl = @"https://www.baidu.com/";
NSDictionary *param = nil;
CjhWeakSelf;
[self simulateSendPacketWithUrl:httpUrl param:param success:^(id responseObj) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
} failure:^(NSError *error) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
}];
//[_condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:_conditionTimeOut]];
}
- (void)requestBasicData {
NSString *httpUrl = @"https://www.baidu.com/";
NSDictionary *param = nil;
CjhWeakSelf;
[self simulateSendPacketWithUrl:httpUrl param:param success:^(id responseObj) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
} failure:^(NSError *error) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
}];
//[_condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:_conditionTimeOut]];
}
- (void)requestServerTime {
NSString *httpUrl = @"https://www.baidu.com/";
NSDictionary *param = nil;
CjhWeakSelf;
[self simulateSendPacketWithUrl:httpUrl param:param success:^(id responseObj) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
} failure:^(NSError *error) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
}];
//[_condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:_conditionTimeOut]];
}
- (void)requestDownloadAdImgData {
NSString *httpUrl = @"https://www.baidu.com/";
NSDictionary *param = nil;
CjhWeakSelf;
[self simulateSendPacketWithUrl:httpUrl param:param success:^(id responseObj) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
} failure:^(NSError *error) {
//do something.
//[weakSelf.condition signal];
dispatch_semaphore_signal(weakSelf.semaphore);
}];
//[_condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:_conditionTimeOut]];
}
#pragma mark - SimulateSendPacketMethod
- (void)simulateSendPacketWithUrl:(NSString *)urlStr param:(NSDictionary*)param success:(void(^)(id responseObj))success failure:(void(^)(NSError *error))failure {
//产生1位的随机数(用于模拟请求所花的时长)。
NSInteger waitSecs = arc4random_uniform(10);
if (waitSecs <= 0) {
waitSecs = 1;
}
CjhLog(@"-[%@] simulate waitSecs=%ld.", self.class, (long)waitSecs);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:waitSecs];
if (waitSecs < 5) {//模拟成功回调。
CjhLog(@"-[%@] simulate callback succ.", self.class);
dispatch_async(dispatch_get_main_queue(), ^{
if (success != NULL) {
success(nil);
}
});
} else {//模拟失败回调。
CjhLog(@"-[%@] simulate callback failed.", self.class);
dispatch_async(dispatch_get_main_queue(), ^{
if (failure != NULL) {
failure(nil);
}
});
}
});
}
@end
网友评论