简体   繁体   English

在iOS上使用MKNetworkKit流式传输

[英]Streaming with MKNetworkKit on iOS

I'm trying to figure out how to make MKNetworkKit working with data from stream. 我试图弄清楚如何使MKNetworkKit处理流中的数据。 I can see that some data is beeing downloaded (the indicator on status bar), but I don't have any idea what happens with that data after it's actually downloaded. 我可以看到正在下载一些数据(状态栏上的指示器),但是我不知道在实际下载这些数据后会发生什么。 I put a NSLog statement inside body of connection: didReceiveData: but it's not called during streaming. 我在connection: didReceiveData:主体中放了一条NSLog语句connection: didReceiveData:但在流传输过程中未调用它。 Any pointers how to fix that issue ? 任何指针如何解决该问题?

Edit 编辑

Sorry my question was inaccurate. 抱歉,我的问题不正确。 I know how to stream to a file but I need to stream to memory (NSData instance preferably). 我知道如何流式传输到文件,但我需要流式传输到内存(最好是NSData实例)。 Okay it seems simple again due to NSOutputStream method initWithBytes:capacity: . 好的,由于NSOutputStream方法initWithBytes:capacity: :,再次看起来很简单。 And my problem is here, my stream has undefined length so there would be enormous impact on memory. 我的问题在这里,我的流具有不确定的长度,因此会对内存产生巨大影响。 I don't know what to do. 我不知道该怎么办。 My perfect solution works like this. 我的完美解决方案是这样的。 Small chunks of data from the stream are processed having been downloaded and then they are discarded. 流中的小数据块已被下载处理,然后将其丢弃。

You could use the outputStreamToBuffer:capacity: method to create the stream. 您可以使用outputStreamToBuffer:capacity:方法创建流。

As for the buffer, you can use a circular buffer, so that you can read from it as the stream writes to it. 至于缓冲区,您可以使用循环缓冲区,以便您可以在流写入缓冲区时从缓冲区中读取缓冲区。 A great implementation (and explanation) is here . 一个很好的实现(和解释)在这里

Streaming a file download is a three line magic with MKNetworkKit. 使用MKNetworkKit,流传输文件下载是三行魔术。

//Create a MKNetworkOperation for the remote URL.

MKNetworkOperation *op = [self operationWithURLString:remoteURL 
                                                    params:nil
                                              httpMethod:@"GET"];

// add your output stream, in this case a file
[op addDownloadStream:[NSOutputStream outputStreamToFileAtPath:filePath
                                                             append:YES]];
// enqueue the operation to a MKNetworkEngine.
[self enqueueOperation:op];

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM