解决方案用于监控和维护光盘上的应用程序大小 [英] Solution For Monitoring and Maintaining App's Size on Disc

查看:95
本文介绍了解决方案用于监控和维护光盘上的应用程序大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,大量使用CoreData和我的很多模型有UIImage和NSData属性(图像和视频)。因为将这些数据直接存储到CoreData中并不是一个好主意,所以我建立了一个文件管理器类,将文件写入文档目录中的不同桶中,具体取决于创建的上下文和媒体类型。

I'm building an app that makes extensive use of CoreData and a lot of my models have UIImage and NSData properties (for images and videos). Since it's not a great idea to store that data directly into CoreData, I built a file manager class that writes the files into different buckets in the documents directory depends on the context in which was created and media type.

我现在的问题是如何管理文档目录?有没有办法来检测应用程序已用尽了其总分配空间多少空间?此外,清除这些目录的最好方法是什么?

My question now is how do I manage the documents directory? Is there a way to detect how much space the app has used up out of its total allocated space? Additionally, what is the best way to go about cleaning those directories; do I check every time a file is written or only on app launch, ect ect.

推荐答案


有没有办法检测应用程序在其分配的总空间中占用了多少空间?

Is there a way to detect how much space the app has used up out of its total allocated space?

应用程序没有对总分配空间的限制,它们受设备上的空间量的限制。您可以通过使用 NSFileManager 来扫描这些文件来了解这些文件使用多少空间。有几种方法以不同的方式做到这一点 - 例如检出 enumeratorAtPath:。对于每个文件,使用 attributesOfItemAtPath:error:的方法来获取文件大小。

Apps don't have a limit on total allocated space, they're limited by the amount of space on the device. You can find out how much space you're using for these files by using NSFileManager to scan the directories. There are several methods that do this in different ways-- check out enumeratorAtPath:, for example. For each file, use a method like attributesOfItemAtPath:error: to get the file size.

在创建和删除文件时跟踪文件大小。保持运行总计,存储在用户默认值。创建新文件时,将其增加新数据量。

Better would be to track the file sizes as you create and delete files. Keep a running total, stored in user defaults. When you create a new file, increase it by the amount of new data. When you remove a file, decrease the running total.


此外,清除这些目录的最佳方法是什么?

Additionally, what is the best way to go about cleaning those directories; do I check every time a file is written or only on app launch, ect ect.

如果这些文件是本地数据,那么它们是本地数据的一部分的相关Core Data对象,明智的方法是在删除文件时删除其Core Data对象。托管对象需要数据文件,因此如果仍在使用对象,请不要删除该文件。这意味着必须有一些方法来链接这两个,但我假设已经是真的,因为你说这些文件被托管对象以某种方式使用。

If these files are local data that's inherently part of the associated Core Data object, the sensible approach is to delete a file when its Core Data object is deleted. The managed object needs the data file, so don't delete the file if you still use the object. That means there must be some way to link the two, but I'm assuming that's already true since you say that these files are used by managed objects somehow.

如果文件是类似缓存的数据,很容易重新创建或重新下载,您应该将它们放在 NSTemporaryDirectory()返回的位置。然后,当它认为需要空间时,iOS可以删除它们。您还可以通过扫描较旧的文件或一段时间没有使用的文件(详细信息取决于您如何使用文件),以适当的方式清除旧文件。

If the files are something like cached data that's easily re-created or re-downloaded, you should put them in the location returned by NSTemporaryDirectory(). Then iOS can delete them when it thinks the space is needed. You can also clear out old files whenever it seems appropriate, by scanning for older files or ones that haven't been used in a while (the details depend on exactly how you use the files).

这篇关于解决方案用于监控和维护光盘上的应用程序大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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