[英]iOS: NSFileHandle vs NSOutputStream for large file download
On iOS, our application is downloading a zip file that's approximately 400MB. 在iOS上,我们的应用程序正在下载大约400MB的zip文件。 We're getting intermittent crashing while the file is downloading.
文件下载时我们会出现间歇性崩溃。
Current I'm using [NSFileHandle writeData:] to write the data as it comes in, and is not being stored in memory. 当前我正在使用[NSFileHandle writeData:]来写入数据,而不是存储在内存中。 But I'm wondering if the operating system is somehow storing it in memory?
但我想知道操作系统是否以某种方式将其存储在内存中?
Would NSOutputStream be a better solution to downloading large file? NSOutputStream会成为下载大文件的更好解决方案吗? Or possibly standard unix file descriptors?
或者可能是标准的unix文件描述符?
Our file handle is setup like this: 我们的文件句柄设置如下:
NSFileManager * fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:tmpFilePath.path contents:nil attributes:nil];
_zipFile = [NSFileHandle fileHandleForWritingAtPath:tmpFilePath.path];
Currently my NSURLConnection delegate method looks like this: 目前我的NSURLConnection委托方法如下所示:
- (void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
[_zipFile writeData:data];
}
So the data that comes in from the request is not stored or appended to any other data objects. 因此,请求中的数据不会存储或附加到任何其他数据对象。 Shouldn't this just write to disk and not effect memory?
这不应该写入磁盘而不影响内存吗?
Thanks 谢谢
I don't think there's anything wrong with your use of NSFileHandle
. 我不认为您使用
NSFileHandle
有任何问题。 I confess that I've always used NSOutputStream
, but I just tried it both ways ( NSFileHandle
and NSOutputStream
), and both appear to be quite respectful in terms of the memory consumed. 我承认我一直使用
NSOutputStream
,但我只是尝试了两种方式( NSFileHandle
和NSOutputStream
),并且两者在消耗的内存方面似乎都非常尊重。 Downloading a 40mb file, the allocations spiked to 3mb at the start of the download but quickly leveled off at 1mb): 下载一个40mb的文件,分配在下载开始时飙升到3mb,但很快稳定在1mb):
So, I'd run your app through "Allocations" and "Leaks" (if you choose "Leaks", you get both) and see what it looks like. 所以,我会通过“Allocations”和“Leaks”运行你的应用程序(如果你选择“Leaks”,你会得到两者)并看看它的样子。
If you haven't already, run your code through the static analyzer ("Analyze" on the "Product" menu), to make sure you don't have any issues. 如果您还没有,请通过静态分析器运行代码(“产品”菜单上的“分析”),以确保您没有任何问题。 (You should have a clean bill of health there; if there are any issues reported, you must fix those.) I'd also make sure that zombies are turned off (because in the process of keeping track of all of those released objects, it creates a zombie object for each ... small, but it will consume memory.
(你应该在那里有一个干净的健康状况;如果报告有任何问题,你必须修复它们。)我还要确保僵尸被关闭(因为在跟踪所有这些被释放的对象的过程中,它为每个......创建一个僵尸对象,但它会消耗内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.