需求:異步上傳5張圖片到服務(wù)器A,上傳成功后將返回的url以及相關(guān)信息傳給服務(wù)器B谨究⌒夂颍【保證 上傳給B的url順序與圖片的上傳順序相同】
方法1:
- (void)semaphoreA{
NSLog(@"============= semaphoreA =======");
NSArray *arr = @[@"aaa",@"bbb",@"ccc",@"ddd"];
//有同時(shí)被寫入數(shù)據(jù)的情況 需要加線程鎖 此處用dispatch_semaphore_t鎖住 以保證數(shù)據(jù)安全
NSMutableArray *addedArray = [NSMutableArray array];
dispatch_semaphore_t semaLock = dispatch_semaphore_create(1);
NSMutableArray *replaceArray = [NSMutableArray arrayWithArray:arr];
for (int i = 0; i < arr.count; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSObject *obj = [arr objectAtIndex:i];
sleep(2);
NSLog(@"image %@ index %tu has uploaded",obj,i);
NSString *newObj = [NSString stringWithFormat:@"%@_%tu",obj,i];
[replaceArray replaceObjectAtIndex:i withObject:newObj];
dispatch_semaphore_wait(semaLock, DISPATCH_TIME_FOREVER);
[addedArray addObject:newObj];
dispatch_semaphore_signal(semaLock);
if(addedArray.count == arr.count){
NSLog(@"done");
NSLog(@"replaceArray = %@",replaceArray);
NSLog(@"addedArray = %@",addedArray);
}
});
}
}
方法2.
- (void)semaphoreAA{
NSLog(@"============= semaphoreAA =======");
NSArray *arr = @[@"aaa",@"bbb",@"ccc",@"ddd"];
//有同時(shí)被寫入數(shù)據(jù)的情況 需要加線程鎖 此處用dispatch_semaphore_t鎖住 以保證數(shù)據(jù)安全
NSMutableArray *addedArray = [NSMutableArray array];
dispatch_semaphore_t semaLock = dispatch_semaphore_create(1);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableArray *replaceArray = [NSMutableArray arrayWithArray:arr];
for (int i = 0; i < arr.count; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSObject *obj = [arr objectAtIndex:i];
sleep(2);
NSLog(@"image %@ index %tu has uploaded",obj,i);
NSString *newObj = [NSString stringWithFormat:@"%@_%tu",obj,i];
[replaceArray replaceObjectAtIndex:i withObject:newObj];
dispatch_semaphore_wait(semaLock, DISPATCH_TIME_FOREVER);
[addedArray addObject:newObj];
dispatch_semaphore_signal(semaLock);
if(addedArray.count == arr.count){
dispatch_semaphore_signal(sema);
}
});
}
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
NSLog(@"done");
NSLog(@"replaceArray = %@",replaceArray);
NSLog(@"addedArray = %@",addedArray);
}
用兩個(gè)數(shù)組是為了顯示 add方法圖片順序問題。
在實(shí)際的上傳圖片網(wǎng)絡(luò)請(qǐng)求中,往往會(huì)有成功&失敗兩個(gè)回調(diào),用上述方法都需要一個(gè)計(jì)數(shù)量判斷,麻煩易出錯(cuò)不說還顯得很不美觀笛辟,于是有了方法3.
方法3:
- (void)groupA{
NSLog(@"============= groupA =======");
dispatch_group_t group = dispatch_group_create();
NSArray *arr = @[@"aaa",@"bbb",@"ccc",@"ddd"];
//有同時(shí)被寫入數(shù)據(jù)的情況 需要加線程鎖 此處用dispatch_semaphore_t鎖住 以保證數(shù)據(jù)安全
NSMutableArray *addedArray = [NSMutableArray array];
dispatch_semaphore_t semaLock = dispatch_semaphore_create(1);
NSMutableArray *replaceArray = [NSMutableArray arrayWithArray:arr];
for (int i = 0; i < arr.count; i++) {
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSObject *obj = [arr objectAtIndex:i];
sleep(2);
NSLog(@"image %@ index %tu has uploaded",obj,i);
NSString *newObj = [NSString stringWithFormat:@"%@_%tu",obj,i];
[replaceArray replaceObjectAtIndex:i withObject:newObj];
dispatch_semaphore_wait(semaLock, DISPATCH_TIME_FOREVER);
[addedArray addObject:newObj];
dispatch_semaphore_signal(semaLock);
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
});
}
//會(huì)阻礙主線程
// dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"done");
NSLog(@"replaceArray = %@",replaceArray);
NSLog(@"addedArray = %@",addedArray);
});
}
如果是成功失敗回調(diào),則
success{
dispatch_semaphore_signal(sema);
}failure{
dispatch_semaphore_signal(sema);
}