我怎样才能得到我的数据块中使用NSURLConnection的的sendAsynchronousRequest的方法? [英] How can I receive my data in pieces with using NSURLConnection's sendAsynchronousRequest method?

查看:104
本文介绍了我怎样才能得到我的数据块中使用NSURLConnection的的sendAsynchronousRequest的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发送一个同步请求与NSURLConncetion

When I send a synchronous request with NSURLConncetion

[NSURLConnection initWithRequest:myRequest delegate:self];

我可以接受我的下载数据块用下面的方法

I can receive my downloaded data in pieces with the following method

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [self.videoData appendData:data];
    NSLog(@"APPENDING DATA %@",data);

}

这样做的好处是,我可以下载大文件时直接写我的数据到一个文件中,并限制RAM的使用。

The advantage of this is that I can write my data directly to a file, and limit ram usage when downloading large files.

当我发送一个异步请求,我怎么能收到我的数据块?我看到还给我的数据的唯一地方是在请求完成处理程序。

When I send an asynchronous request, how can I receive my data in pieces? The only place I see the data given back to me is in the completion handler of the request.

 [NSURLConnection sendAsynchronousRequest:videoRequest 
                                    queue:downloadQueue 
                        completionHandler:^(NSURLResponse* response, NSData* data, NSError* error){
        NSLog(@"All data is given here!");
    }];

有没有办法解决这个问题?我下载大文件的视图控制器,并希望如果视图控制器被驳回,继续下载它们。问题是,我会,如果我下载大文件时收到我的所有数据一次使用太多内存。

Is there any solution to this problem? I'm downloading large files in a view controller and want to continue downloading them if the view controller gets dismissed. The problem is that I'm going to use too much memory if I receive all my data at once when downloading large files.

推荐答案

在NSURLConnection的唯一方法,是同步的是 + sendSynchronousRequest:returningResponse:错误:

The only method in NSURLConnection which is synchronous is + sendSynchronousRequest:returningResponse:error:

下面的方法都是异步的。

The following methods are all asynchronous

+ connectionWithRequest:delegate:
– initWithRequest:delegate:
– initWithRequest:delegate:startImmediately:
+ sendAsynchronousRequest:queue:completionHandler:
– start

所以,code [NSURLConnection的initWithRequest:myRequest委托:自我]; 本身就是一个异步的,你可以使用它,因为它是

So the code [NSURLConnection initWithRequest:myRequest delegate:self]; itself is an asynchronous, You can use it as it is.

您可以使用 NSURLSession 的更多控制。

You can make use of NSURLSession for more control

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

self.downloadTask = [self.session downloadTaskWithRequest:request];
[self.downloadTask resume];

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {

}

这篇关于我怎样才能得到我的数据块中使用NSURLConnection的的sendAsynchronousRequest的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆