使用AFNetworking批量上传视频

时间:2014-02-19 06:14:13

标签: ios networking post upload afnetworking

我正在尝试让AFNetworking(1.x)将一些长视频批量上传到我的服务器,但是当我使用标准功能时,应用程序的内存会达到400MB,然后退出。

NSMutableArray *requests = [[NSMutableArray alloc] init];

NSMutableURLRequest *request = [[MyServerClient sharedClient] multipartFormRequestWithMethod:@"POST" path:URL parameters:@{ @"json": jsonString } constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileData:mediaData name:idString fileName:fileName mimeType:mimeType];
            }];

[request setTimeoutInterval:10800];

[requests addObject:operation];


[[MYServerClient sharedClient] enqueueBatchOfHTTPRequestOperationsWithRequests:requests   progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {

        NSLog(@"Number - %d %d", numberOfCompletedOperations, totalNumberOfOperations);

    } completionBlock:^(NSArray *operations) {
        NSLog(@"Completion block");
    }];

MyServerClient是AFHTTPClient的一个非常标准的子类:

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];

    [self.operationQueue setMaxConcurrentOperationCount:1];

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

    return self;
}

我猜测内存崩溃可能与应用程序无法控制并发操作量有关,但鉴于设置setMaxConcurrentOperationCount,我不确定。有没有人有任何想法为什么会发生这种情况?

编辑:我的猜测是崩溃是由于应用程序在将媒体附加到多部分请求之前尝试将媒体加载到内存中。在这个POST使用场景中是否有某种方法可以从磁盘上传流上传?

1 个答案:

答案 0 :(得分:0)

对于遇到同样问题的人,以下内容似乎可以解决我的崩溃问题:

NSMutableURLRequest *request = [[MyServerClient sharedClient] multipartFormRequestWithMethod:@"POST" path:URL parameters:@{ @"json": jsonString } constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileURL:mediaURL name:idString error:nil];
            }];