欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS開(kāi)發(fā)中異步網(wǎng)絡(luò)請(qǐng)求上實(shí)現(xiàn)同步邏輯

 更新時(shí)間:2017年03月20日 09:54:48   作者:余成海  
這篇文章主要介紹了IOS開(kāi)發(fā)中異步網(wǎng)絡(luò)請(qǐng)求上實(shí)現(xiàn)同步邏輯的相關(guān)資料,需要的朋友可以參考下

IOS開(kāi)發(fā)中異步網(wǎng)絡(luò)請(qǐng)求上實(shí)現(xiàn)同步邏輯

前提:

可能遇到一些問(wèn)題,比如上傳多個(gè)數(shù)據(jù),需要等多個(gè)數(shù)據(jù)上傳成功后做一定的處理,而且一個(gè)個(gè)上傳,萬(wàn)一哪個(gè)上傳失敗了,后面就不需要上傳了,直接報(bào)錯(cuò)。

之前ASI的網(wǎng)絡(luò)庫(kù)中是有同步請(qǐng)求的接口,所以很好處理,AFNetwork的網(wǎng)絡(luò)庫(kù)只有異步的網(wǎng)絡(luò)請(qǐng)求,該怎么實(shí)現(xiàn)呢?

1.循環(huán)異步拼組

- (void)uploadFile:(NSArray *)imageArray atIndex:(NSInteger)index imagesCount:(NSInteger)count completeBlock:(uploadCompleteBlock)block {
 FNCircleImage *aTCImage = imageArray[index];
 NSString *filepath = aTCImage.localFilePath;
 [self.resourceManager upload:filepath progress:nil completion:^(NSString * _Nullable urlString, NSError * _Nullable error) {
  if (error == nil) {
   aTCImage.remoteUrl = urlString;

   NSInteger idx = index + 1;
   if (idx >= count) {
    block(nil);
   } else {
    [self uploadFile:imageArray atIndex:idx imagesCount:count completeBlock:block];
   }
  } else {
   block(error);
  }
 }];
}

2.信號(hào)量異步轉(zhuǎn)同步

__block NSError *e = nil;
[imageArray enumerateObjectsUsingBlock:^(NSString *filePath, NSUInteger idx, BOOL * _Nonnull stop) {
 __block dispatch_semaphore_t t = dispatch_semaphore_create(0);
 [self upload:filepath progress:nil completion:^(NSString * _Nullable urlString, NSError * _Nullable error) {
  if (error == nil) {
   
  } else {
   e = error;
   *stop = YES;
  }
  dispatch_semaphore_signal(t);
 }];
 dispatch_semaphore_wait(t, DISPATCH_TIME_FOREVER);
}];

3.NSOperationQueue可控隊(duì)列

1).繼承NSOperation實(shí)現(xiàn)上傳邏輯,完成發(fā)出通知或者block回調(diào)

2).用上傳數(shù)據(jù)創(chuàng)建Operation數(shù)組,加入NSOperationQueue中執(zhí)行

3).根據(jù)完成回調(diào)的結(jié)果和個(gè)數(shù)判斷結(jié)果,如果中間有失敗,可以關(guān)閉未執(zhí)行的Operation

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論