GCDAsyncsocket 使用非同步序列子執行緒中for循環執行一半就不執行了,程式碼如下,過程是點擊傳送按鈕回到主頁面,然後非同步執行下面的程式碼上傳圖片;
- (void)sendAction{
dispatch_queue_t queue = dispatch_queue_create("uploadImage", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i =0; i < 10; i++) {
NSLog(@"async ------------ %@", [NSThread currentThread]);
//第一张图的资源
PHAsset *asset = self.sendImageArray[i];
NSString *fileType;
NSString *fileName;
if (asset.mediaType == PHAssetMediaTypeImage) {
fileType = @"jpg";
NSData *data = [NSData dataWithContentsOfFile:filePath[i]];
[self upLoadImageData:data type:@"jpg" name:fileName];
} else if(asset.mediaType == PHAssetMediaTypeVideo) {
fileType = @"mp4";
NSData *data = [NSData dataWithContentsOfFile:filePath[i]];
[self uploadMp4Data:data type:fileType name:fileName];
}
}
});
}
- (void)upLoadImageData:(NSData*)data type:(NSString *)fileType name:(NSString *)fileName{
dataNameMutStr = [NSMutableString stringWithFormat:@"ios_%@_%@.jpg\n", @"图片",fileName];
NSData *dataName = [dataNameMutStr dataUsingEncoding:NSUTF8StringEncoding];
[self sendSocket:dataName data:data];
}
- (void)sendSocket:(NSData *)dataName data:(NSData *)datas{
[self createClientTcpSocket];//发图片
[_asyncsocket writeData:datas withTimeout:-1 tag:0];
}
資訊不大全,不好判斷,原因,不過像這種情況,應該把for循環放到裡面
還有,資源訪問的時候都應該加個鎖
程式碼和日誌不完整,看不出什麼原因。猜測原因兩:
其實還咋執行,但是調試的問題你抓不到狀態了
真的被中斷了,具體原因要深究,不過建議你把queue做一個引用,長期持有,不作為局部變量,如果局部變量那麼做一個串行隊列的意義在哪裡?