NSURLConnection代表在一种情况下无法正常工作

时间:2015-06-08 22:15:12

标签: ios multithreading delegates nsurlconnection alassetslibrary

我已经阅读了很多有关此问题的帖子,但我无法让回调委托工作......

我已经读过委托不能在后台线程中,或者它不会被调用。我认为这可能是问题......但是......好吧,让我在这里加上一些代码来解释我的问题并看看你们的想法。

主线程中的

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    @try {
                        ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
                        [assetLibrary assetForURL:[object valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset)
                         {
                             ALAssetRepresentation *rep = [asset defaultRepresentation];
                             Byte *buffer = (Byte*)malloc(rep.size);
                             NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
                             NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
                             //[data writeToFile:photoFile atomically:YES];//you can save image later
                             UIImage *image = [UIImage imageWithData:data];

                             CGFloat compression = 0.9f;
                             CGFloat maxCompression = 0.5f;
                             int maxFileSize = 1280*720; //ou usar 40960 = 40MB ?

                             NSData *imageData = UIImageJPEGRepresentation(image, compression);

                             while ([imageData length] > maxFileSize && compression > maxCompression)
                             {
                                 compression -= 0.1;
                                 imageData = UIImageJPEGRepresentation(image, compression);
                             }

                             NSString *guid = [self.idMediasAvailable lastObject];
                             //NSString *guid = [[NSUUID UUID] UUIDString];
                             NSString *boundary = @"-endUploadBoundary-";
                             NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

                             [client addBlobToContainer:[container objectAtIndex:self.mycontainerIndex] blobName:[NSString stringWithFormat:@"%@.jpg", guid] contentData:imageData contentType:contentType ];

                             [uploadedMediaID setObject:object forKey:guid];
                             [confirmMedias addObject:guid];
                             [listUploadedMedias addObject:object];
                             [self.idMediasAvailable removeLastObject];

                             //             [allUploadedLock lock];
                             //             [allUploadedLock unlockWithCondition:0];
                         }
                                     failureBlock:^(NSError *err) {
                                         NSLog(@"Error: %@",[err localizedDescription]);
                                         [allUploadedLock lock];
                                         [allUploadedLock unlockWithCondition:0];
                                     }];

                    }
                    @catch (NSException *exception) {
                        NSLog(@"Error uploadMedia: %@",[object valueForKey:UIImagePickerControllerReferenceURL]);
                    }
                });

                [allUploadedLock lockWhenCondition:0];
                [allUploadedLock unlock];

这是addToBlobContainer选择器:

- (void)addBlobToContainer:(BlobContainer *)container blobName:(NSString *)blobName contentData:(NSData *)contentData contentType:(NSString*)contentType withBlock:(void (^)(NSError*))block
{
CloudURLRequest* request = nil;

if(_credential.usesProxy)
{
    request = [_credential authenticatedRequestWithEndpoint:@"/SharedAccessSignatureService/blob" forStorageType:@"blob" httpMethod:@"PUT" contentData:contentData contentType:contentType, @"x-ms-blob-type", @"BlockBlob", nil];
}
else
{
    NSString* containerName = [container.name lowercaseString];
    NSString* endpoint = [NSString stringWithFormat:@"/%@/%@", [containerName URLEncode], [blobName URLEncode]];
    request = [_credential authenticatedRequestWithEndpoint:endpoint forStorageType:@"blob" httpMethod:@"PUT" contentData:contentData contentType:contentType, @"x-ms-blob-type", @"BlockBlob", nil];
}

[request fetchNoResponseWithBlock:^(NSError* error)
 {
     if(error)
     {
         if(block)
         {
             block(error);
         }
         else if([(NSObject*)_delegate respondsToSelector:@selector(storageClient:didFailRequest:withError:)])
         {
             [_delegate storageClient:self didFailRequest:request withError:error];
         }
         return;
     }

     if(block)
     {
         block(nil);
     }
     else if([(NSObject*)_delegate respondsToSelector:@selector(storageClient:didAddBlobToContainer:blobName:)])
     {
         [_delegate storageClient:self didAddBlobToContainer:container blobName:blobName];
     }
 }];

}

在块内没有遇到断点[request fetchNoResponseWithBlock:(NSError * error)...];

这是fetchNoResponseWithBlock:selector:

- (void) fetchNoResponseWithBlock:(noResponseBlock)block {
    _noResponseBlock = [block copy];
    [NSURLConnection connectionWithRequest:self delegate:self];
}

在同一个文件中我有委托选择器

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

并且这些都没有被击中...顺便说一下接口声明是@interface CloudURLRequest:NSMutableURLRequest

好的,现在奇怪的是委托选择器内的断点 当页面打开并且另一个请求获取Blob容器时被点击...但是当我尝试发送文件时,文件被发送并且没有回调! (我手动检查blob并成功上传)

我认为当我这样做时:     [assetLibrary assetForURL:[object valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {

这个块是否有机会在后台线程上运行?还是有什么建议?

我确实需要回调才能工作,因为我必须在文件完成上传时解锁NSLock。

提前多多感谢!

0 个答案:

没有答案