简体   繁体   English

iOS:NSFileHandle与NSOutputStream进行大文件下载

[英]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 ,但我只是尝试了两种方式( NSFileHandleNSOutputStream ),并且两者在消耗的内存方面似乎都非常尊重。 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.

相关问题 NSFileHandle和编写Asynch到iOS中的文件 - NSFileHandle & Writing Asynch to a file in iOS iOS ALAssetsLibrary 和 NSFileHandle 读取文件内容 - iOS ALAssetsLibrary and NSFileHandle reading file contents 为大型文件和小型文件创建NSFileHandle之间有性能差异吗? - Is there any performance difference between creating an NSFileHandle for a large versus a small file? 下载大文件时应用崩溃-NSFileHandle seekToEndOfFile - App crashing when downloading a large file - NSFileHandle seekToEndOfFile NSFilehandle逐行文件读取器上的iOS内存泄漏 - iOS Memory Leak on NSFilehandle line-by-line File Reader iOS:下载,解包,解码和解析大文件 - iOS: Download, unwrap, decode, and parse a large file Google Drive SDK iOS-使用NSData(不是NSFileHandle)上传大视频ALAssetRepresentation吗? - Google Drive SDK iOS - Upload LARGE video ALAssetRepresentation using NSData (not NSFileHandle)? ios:应用处于后台或挂起状态时下载大文件 - ios: Download large file when app is in background or suspended state iOS:这是从服务器下载大文件的最佳方法 - IOS: Which is the best way to download a Large size file from server 从iOS下载具有恢复功能的大文件(1 + Gb) - Download a large file (1+Gb) from iOS with resume capability
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM