AFNetworking 2.0을 사용하여 스트리밍 요청에 직면하는 몇 가지 문제점이 있습니다.업로드 진행률을 제공 할 수있는 단일 스트리밍 요청으로 AFNetworking을 사용하여 서버에 최대 100 개의 파일을 업로드해야합니다.
많은 파일 (~ 50MB)을 서버에 업로드하려고하며 요청을 스트리밍해야합니다. (그렇지 않으면 메모리 압력으로 인해 응용 프로그램이 충돌합니다.)
나는 AFNetworking 2.0에서 어떤 성공도없이 여러 가지 방법으로 시도했습니다. 이 잘 작동
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:baseServiceUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int imageIndex = 0;
for (NSURL *tempFileUrl in tempFilesUrlList) {
NSString* fileName = [NSString stringWithFormat:@"image%d", imageIndex];
NSError *appendError = nil;
[formData appendPartWithFileURL:tempFileUrl name:fileName error:&appendError];
imageIndex++;
}
} error:&error];
AFHTTPRequestOperation *requestOperation = nil;
requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
DLog(@"Uploading files completed: %@\n %@", operation, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(@"Error: %@", error);
}];
[requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
double percentDone = (double)totalBytesWritten/(double)totalBytesExpectedToWrite;
DLog(@"progress updated(percentDone) : %f", percentDone);
}];
[requestOperation start];
있지만 스트리밍하지 : 이것은 내가 현재하고있는 중이 것입니다. 요청이 너무 크면 메모리에서 요청을 충돌시킬 수 있습니다. 나는 네이티브 소켓과 스트림을 실험했지만, 애플은 그것 때문에 앱을 비 승인 할 것이라고 말한다.
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
NSString *appID = [[NSBundle mainBundle] bundleIdentifier];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:appID];
NSURL* serviceUrl = [NSURL URLWithString:baseServiceUrl];
manager = [manager initWithBaseURL:serviceUrl sessionConfiguration:configuration];
NSURLSessionDataTask *uploadFilesTask = [manager POST:baseServiceUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int imageIndex = 0;
for (NSURL *tempFileUrl in tempFilesUrlList) {
NSString* fileName = [NSString stringWithFormat:@"image%d", imageIndex];
NSError *appendError = nil;
[formData appendPartWithFileURL:tempFileUrl name:fileName error:&appendError];
imageIndex++;
}
} success:^(NSURLSessionDataTask *task, id responseObject) {
DLog(@"Files uploaded successfully: %@\n %@", task, responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
DLog(@"Error: %@", error);
}];
[progressBar setProgressWithUploadProgressOfTask:uploadFilesTask animated:true];
을하지만 내가 말하는 시간 오류를 실행 제공 :
내가 시도 또 다른 방법은 다음 인해 캐치되지 않는 예외 'NSGenericException', 이유에
응용 프로그램을 종료 '업로드 작업 백그라운드 세션에서 파일 ' ***에서 첫 번째 호출 스택을 던져야합니다 : (0 CoreFoundation 0x02cc75e4 __exceptionPreprocess + 180 1 libobjc.A.dylib
0x02a4a8b6 objc_exception_throw + 44 2 CFNetwork에서
0x0459eb6c는 - [__ NSCFBackgroundSessionBridge는 uploadTaskForRequest는 :을 UploadFile : bodyData : 완료 :] + 994 3
CFNetwork에서의 0x045f2f37 - [__ NSCFURLSession uploadTaskWithStreamedRequest :] +
73 ( 스트리밍 이어야하며 계속해서 배경)
iOS는 백그라운드 업로드로 스트리밍을 지원하지 않습니다. 백그라운드에서 지원해야합니까, 아니면 전경에서만 지원해야합니까? 포어 그라운드에서 이론 상으로는 스트리밍 할 수 있어야합니까? 다른 옵션은 서버 측 지원을보고 작은 부분으로 파일을 업로드하는 것입니다. 서버가 다중 부분 업로드 (즉, Amazon S3의 다중 부분 업로드 http://docs.aws.amazon.com/AmazonS3/latest/dev/mpuoverview.html)를 지원합니까? 부품을 사용하면 메모리 오버 헤드를 피하는 작은 부분에 파일을 업로드 할 수 있습니다. –