Available Storage displayed in iOS setting is not correct

Hi~recently I have storage issue as follows:

Go to iOS Settings -> iPhone Storage , it shows available  storage  is enough (ex.180.38 GB)

But app will get disk full error when trying to write large file (ex. 26 GB)

Error Domain=NSCocoaErrorDomain Code=640 "The file couldn’t be saved because there isn’t enough space." UserInfo={NSUnderlyingError=0x282db8ae0 {Error Domain=NSPOSIXErrorDomain Code=28 "No space left on device"}}

Then I write the following  code to log available space and found it is only 26.95 GB available not 180.38 GB as UI displayed

NSError *error = nil;
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:path error: &error];
NSNumber *sizeValue = [dictionary objectForKey:NSFileSystemFreeSize];
uint64_t totalFreeSpace = [sizeValue unsignedLongLongValue];
print(@“ %@ Free space available.", [NSByteCountFormatter stringFromByteCount:totalFreeSpace countStyle:NSByteCountFormatterCountStyleFile]);

Does any one know what reason  may cause this strange situation and how can I do to make available space displayed in UI could be reliable?

Any suggestion will be very appreciated!

Replies

This is a complex problem. There is no single number that represents the amount of free disk space. There are a variety of reasons for this, but the biggest one is that the system can cache information. So:

  • There’s the amount of free disk that’s available on the file system right now.

  • And the amount of free disk space that can be made available by purging caches.

You’re currently using the old school NSFileManager API (NSFileSystemFreeSize, aka .systemFreeSize), which isn’t set up to deal with the subtlety. I recommend that you switch to the new NSURL APIs:

  • NSURLVolumeAvailableCapacityKey (.volumeAvailableCapacityKey in Swift) is documented here. This is equivalent to the old API.

  • NSURLVolumeAvailableCapacityForImportantUsageKey (.volumeAvailableCapacityForImportantUsageKey) is documented here.

  • NSURLVolumeAvailableCapacityForOpportunisticUsageKey (.volumeAvailableCapacityForOpportunisticUsageKey) is documented here.

Note It’s likely that none of these will yield exactly the same results as you get in Settings. There is no API to get exactly the values you see in user-level locations like Settings, Xcode, and Finder. Each of these calculates usage in their own unique way.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi eskimo,

Thanks for your kindly sharing.

I read the document but still be confused. Although I use old api to print available space, but I did get the disk full error when I saving a video to disk which size 26GB is much less than 180GB shows in iOS Settings.

I would like to ask why this situation happens and what error handling should I do?

  • According to the information you mentioned before, is it means that sytem cache about 150 GB information so I can not write 26GB video to disk?
  • If it does, how should I do to handle this error to make my write action could be succeed?
    • Will it help if I wait several seconds and try writing again?
    • Or can I do any action to force system purge cache ASAP?

Thanks~