Swift Applications Takes More Space on Disk

Swift Applications takes more space on disk

Until the Swift runtime and libraries are included in the operating system, and you're willing to drop support for all operating systems prior to that version, you'll have to include them in your app like this.

Query Available iOS Disk Space with Swift

iOS 11 Update

The answers given below no longer provide accurate results under iOS 11. There are new volume capacity keys that can be passed to URL.resourceValues(forKeys:) that provide values that match what is available in device settings.

  • static let volumeAvailableCapacityKey: URLResourceKey
    Key for the volume’s available capacity in bytes (read-only).

  • static let volumeAvailableCapacityForImportantUsageKey: URLResourceKey
    Key for the volume’s available capacity in bytes for storing important resources (read-only).

  • static let volumeAvailableCapacityForOpportunisticUsageKey: URLResourceKey
    Key for the volume’s available capacity in bytes for storing nonessential resources (read-only).

  • static let volumeTotalCapacityKey: URLResourceKey
    Key for the volume’s total capacity in bytes (read-only).

From Apple's documentation:

Overview

Before you try to store a large amount of data locally, first verify that you have sufficient storage capacity. To get the storage capacity of a volume, you construct a URL (using an instance of URL) that references an object on the volume to be queried, and then query that volume.

Decide Which Query Type to Use

The query type to use depends on what's being stored. If you’re storing data based on a user request or resources the app requires to function properly (for example, a video the user is about to watch or resources that are needed for the next level in a game), query against volumeAvailableCapacityForImportantUsageKey. However, if you’re downloading data in a more predictive manner (for example, downloading a newly available episode of a TV series that the user has been watching recently), query against volumeAvailableCapacityForOpportunisticUsageKey.

Construct a Query

Use this example as a guide to construct your own query:

let fileURL = URL(fileURLWithPath: NSHomeDirectory() as String)
do {
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
if let capacity = values.volumeAvailableCapacityForImportantUsage {
print("Available capacity for important usage: \(capacity)")
} else {
print("Capacity is unavailable")
}
} catch {
print("Error retrieving capacity: \(error.localizedDescription)")
}


Original Answer

Optional binding with if let works here as well.

I would suggest that the function returns an optional Int64, so that it can return
nil to signal a failure:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if let systemAttributes = NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectoryPath.last as String, error: nil) {
if let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber {
return freeSize.longLongValue
}
}
// something failed
return nil
}

Swift 2.1 Update:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
guard
let systemAttributes = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectory),
let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber
else {
// something failed
return nil
}
return freeSize.longLongValue
}

Swift 3.0 Update:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
guard
let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: documentDirectory),
let freeSize = systemAttributes[.systemFreeSize] as? NSNumber
else {
// something failed
return nil
}
return freeSize.int64Value
}

Usage:

if let bytes = deviceRemainingFreeSpaceInBytes() {
print("free space: \(bytes)")
} else {
print("failed")
}

What is the maximum amount of data an iOS app can handle?

Since you're saving the content to disk, the limitation is the disk size of the user's device. It also depends on where you're saving the data: If you save to the tmp directories, the data may get deleted, but if you're saving to the application's normal directory it will also be backed up to iCloud (assuming the user backs up).

I would advise against filling up the user's phone with hundreds of MB of data automatically, they will get very annoyed at you. So you will want to offer them a UI to actually select which content they want to keep and which should be removed.

How to detect total available/free disk space on the iPhone/iPad device?

UPDATE: Since a lot of time has passed after this answer and new methods/APIs have been added, please check the updated answers below for Swift etc; Since I've not used them myself, I can't vouch for them.

Original answer:
I found the following solution working for me:

-(uint64_t)getFreeDiskspace {
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}

return totalFreeSpace;
}

It returns me exactly the size that iTunes displays when device is connected to machine.

iOS - Reserve a minimum amount of free disk space for an app

You can do two things:

First, when your application starts, you can find out the available space using NSFileManager.attributesForFilesystemofPath:. If the amount of space is low (and 5 MB free is really low, you can warn the user and maybe even refuse to start actions in your app that require this available space.

Second, you could create a scratch file. If you know you need 5MB storage, then maybe you can allocate a 5MB file and use that file for your storage.

You don't specify what you need to store on disk but if it were for example one file then you could write this data over your 5MB scratch file instead of opening a new file.

This requires some extra bookkeeping, but you do get guaranteed space for your data.

Why does image in xcode takes up 50x more space during runtime than the actual size

Marc B's comment is right on the money.

You've got it backwards. It doesn't take 50x more memory "in Xcode" (in the running program) than "the actual size".

The file on disk is compressed, and takes a lot less space than the image does in memory. The actual size is the size in memory, and the size on disk is a compressed format for saving, but that can't be drawn to the screen.

To render an image on screen, it needs to use 3 or 4 bytes per pixel (red, green, blue, and sometimes an alpha channel) with no compression. So, if you have a 1000x1000 pixel image, loading it into memory takes 4 million bytes of memory. If you save that to disk, different file formats use varying levels of compression to save that data. One simple scheme is RLE, run-length-encoding. In that scheme, sequential bytes of the same color are written with a special code (e.g. "save 500 pixels of pure blue here," although unless the image is generated with a paint program, there are rarely large areas of exactly the same color. RLE is all but worthless for photographs or scans.)

Other compression schemes achieve much higher levels of compression. Some compression schemes are "lossy" (which means the image you get back after compressing then decompressing the image isn't exactly the same as the starting image.) JPEG compression is an example of a lossy scheme. In JPEG compression you can adjust the level of compression. Higher levels of compression yield much smaller files, but the resulting images don't look as good, because the amount of data lost gets larger and larger.

Regardless of the compression scheme, the graphics hardware needs the full 4-bytes-per-pixel in memory before it can draw it.

P.S. A pet peeve of mine: Xcode is a development environment (editor, compiler, debugger, linker, etc.) Your image isn't drawn "in Xcode". It is rendered in iOS or Mac OS by a program that you happen to compile using Xcode.

(So when people talk about why "Xcode" runs their code a certain way, it makes experienced developers cringe, and makes you sound ignorant. I don't intend to be mean - just letting you know.)

What happens when you overflow iOS app Documents directory?

From my experience of downloading huge amounts of data and pre-caching them in the app's bundle (not in the Documents directory though), there is no filesystem size limit that iOS imposes for the single app.

The only limit is the free disk space of iOS device. Once you reach that limit, iOS triggers a cleanup on all the apps that are installed on the device, cleaning Library/Caches and tmp folders inside the bundle of all installed apps.

I did not test this yet, but there seems to be an NSBundleResourceRequestLowDiskSpace notification that is posted by the system when free disk space gets low.

Is there a way to determine the amount of disk space an app has used in iOS?

I ended up figuring out how to do this:

I created a category on NSFileManager and added:

-(NSUInteger)applicationSize
NSString *appgroup = @"Your App Group"; // Might not be necessary in your case.

NSURL *appGroupURL = [self containerURLForSecurityApplicationGroupIdentifier:appgroup];
NSURL *documentsURL = [[self URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask] firstObject];
NSURL *cachesURL = [[self URLsForDirectory: NSCachesDirectory inDomains: NSUserDomainMask] firstObject];

NSUInteger appGroupSize = [appGroupURL fileSize];
NSUInteger documentsSize = [documentsURL fileSize];
NSUInteger cachesSize = [cachesURL fileSize];
NSUInteger bundleSize = [[[NSBundle mainBundle] bundleURL] fileSize];
return appGroupSize + documentsSize + cachesSize + bundleSize;
}

I also added a category on NSURL with the following:

-(NSUInteger)fileSize
{
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:self.path isDirectory:&isDir];
if (isDir)
return [self directorySize];
else
return [[[[NSFileManager defaultManager] attributesOfItemAtPath:self.path error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
}

-(NSUInteger)directorySize
{
NSUInteger result = 0;
NSArray *properties = @[NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:self includingPropertiesForKeys:properties options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *url in files)
{
result += [url fileSize];
}

return result;
}

It takes a bit to run if you have a lot of app data, but it works.



Related Topics



Leave a reply



Submit