如何获取coredata存储中存在的数据大小? [英] How to get the size of data present in coredata store?

查看:83
本文介绍了如何获取coredata存储中存在的数据大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取核心数据存储中存在的数据的总内存大小(以字节为单位)以及如何获取特定数据行的内存大小.

How to get the total memory size in bytes of data present in core data store.And How to get the memory size for particular rows of data.

推荐答案

在iOS 7及更高版本上,

On iOS 7 and above, older answers don't work anymore for SQLite-backed persistent stores, because of SQLite journal files. For each persistent store file you need to get the size of the SQLite file itself (e.g. Foo.sqlite) and the sizes of the journal files (e.g. Foo.sqlite-wal and Foo.sqlite-shm) and add the sizes to get the total. This is really important because it's very likely that most of the data is actually in the journal files. You can use NSFileManager to get this information for each file.

如果您在模型中使用二进制属性,则为这些属性中的任何一个启用了允许外部存储",它将变得更加复杂.在这种情况下,您需要找到所有外部存储文件,并增加它们的大小.它们的位置没有记录,但是应该在找到SQLite文件的目录的子目录中.对于名为 Foo.sqlite 的SQLite文件,请查找名为 .Foo_SUPPORT/_EXTERNAL_DATA 的目录,外部二进制文件将在那里.由于未对此进行记录,因此如有更改,恕不另行通知.

If you're using binary attributes in your model and you have "Allows External Storage" enabled for any of those attributes, it gets more complicated. In that case you need to find all of the external storage files and add up their sizes too. Their location is not documented but should be in a subdirectory of the directory where the SQLite file is found. For a SQLite file named Foo.sqlite look for a directory named .Foo_SUPPORT/_EXTERNAL_DATA, and the external binary files will be there. Since this is not documented, it may be subject to change without warning.

一种更好的方法(如果还不算太晚的话)是将持久性存储放在其自己的子目录中.与其将其放置在documents目录中,不如在文档内部创建一个新目录,然后将您的SQLite文件放置在该目录中.也就是说,创建一个名为 data 之类的目录,并将 Foo.sqlite 放入其中.要累加大小,只需递归地扫描 data 中的每个文件并累加大小即可.然后,您将捕获所有内容,即使是日记或外部二进制文件,也可能是iOS未来版本中的任何更改.进行扫描的一种好方法是使用 NSFileManager enumeratorAtPath: enumeratorAtURL:includesPropertiesForKeys:options:errorHandler:方法.

A better approach-- if it's not too late-- is to put the persistent store in its own subdirectory. Instead of putting it in the documents directory, create a new directory inside documents and put your SQLite file there. That is, create a directory called something like data and put Foo.sqlite in it. To add up the size, just recursively scan every file in data and add up the sizes. Then you'll catch everything, even if journaling or external binaries or whatever change in future versions of iOS. A good way to do the scan would be with NSFileManager's enumeratorAtPath: or enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: methods.

查找特定数据行的内存大小是一个非常不同的问题,并且没有通用的解决方案.任何对象实例的开销,加上您的属性,再加上可能会创建的所有内部未记录对象 NSManagedObject (包括实例var和动态分配的内存),都将产生开销.它甚至不是固定值,因为可以动态分配和释放数据.只需添加属性的大小就很容易-只需遍历它们,然后将每个属性的大小(字符串长度,数据长度等)相加即可,但这还不是全部.

Finding the memory size for a particular row of data is a very different question, and there's no general solution. There's the overhead of any object instance, plus your attributes, plus any internal undocumented objects NSManagedObject might create (including instance vars and dynamically allocated memory). It's not even a fixed value since data can be allocated and released on the fly. Adding up the sizes of just your attributes is easy-- just run through them and add up the size of each (string length, data length, etc), but it's not the full picture.

这篇关于如何获取coredata存储中存在的数据大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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