I am working on a database application using the Core Data framework. In this application I need to display how much data the application currently is using on the iPhone. Is there any way to do this?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- how do you prevent page scroll in textarea on mobi
相关文章
- Could I create “Call” button in HTML 5 IPhone appl
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Open iOS 11 Files app via URL Scheme or some other
- Converting (u)int64_t to NSNumbers
- Can keyboard of type UIKeyboardTypeNamePhonePad be
Not sure where I saw it, but I believe that removing entries from the database will not necessarily shrink the database file. SQLite reclaims the storage internally and re-uses it. (This is typical of RDBMSs.) I believe there's a command-line utility somewhere that will compact it, but that's not going to help you if your app wants shrink the file to the dataset (to reclaim space for the OS, for example).
So while the file size method will give you a sense of the high-water-mark size of the database, it's not necessarily going to tell you the amount of storage used by your dataset.
Your persistent store in Core Data is just a file on the file system. You access and possibly create this file when you create your Core Data stack. The following code will print the size of a persistent store and the free space of the file system, in bytes:
This assumes that your persistent store is named
persistentstore.sqlite
and is stored in the documents directory for your application. If you are uncertain as to the name of your persistent store, look for where you alloc and init your NSPersistentStoreCoordinator. The name of the store should be specified somewhere in the code around there.Note that the values you get back from the file and file system attributes dictionaries are NSNumbers, so you'll need to convert them to scalar types if you want to work with the file sizes in that manner. One thing to be careful of is that these values are in bytes, so for multi-gigabyte filesystems you might run into number size limitations with 32-bit integer data types.
I found this answer on the Apple Dev Forums to be useful for finding disk space available on the apps' home directory partition (note there are currently two partitions on each device).
Use
NSPersistentStoreCoordinator
to get your store collection.Use
NSFileManager
to get each stores' size in bytes(unsigned long long)
Note that the code above is in a method of my app delegate, and it has a property
persistentStoreCoordinator
.