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];
}
漂亮男人2017-05-02 09:36:17
信息不大全,不好判断,原因,不过像这种情况,应该把for循环放到里面
还有,资源访问的时候都应该加个锁
dispatch_async(queue, ^{
for (int i =0; i < 10; i++) {
// upload ....
}
})
大家讲道理2017-05-02 09:36:17
代码和日志不完整,看不出什么原因。猜测原因两个:
其实还咋执行,但是调试的问题你抓不到状态了
真的被中断了,具体原因要深究,不过建议你把queue做一个引用,长期持有,不作为局部变量,如果局部变量那么做一个串行队列的意义在哪里?