-2

I want my app to know when disk free space changes and update my view. Is there any notification sent by system?

Matt.Z
  • 536
  • 6
  • 17
  • This is almost an exact duplicate of http://stackoverflow.com/questions/5712527/how-to-detect-total-available-free-disk-space-on-the-iphone-ipad-device. Use the strategy in that post to measure free disk space at certain moments, then monitor changes yourself. – Wilbo Baggins Jan 30 '13 at 10:53
  • Please.. Read my question carefully. I want to be notified when the size changes, not asking how to calculate disk space. I definitely saw this post before asking. – Matt.Z Jan 30 '13 at 11:01
  • 2
    There seems to be no good reason why iOS would send notifications to applications when the amount of free space on the device changes. In order to do so timely, it would have to constantly monitor the free space - very inefficient. But luckily, you can monitor it yourself and do something when you notice a change. I've added this as an answer so your question is now officially answered :). – Wilbo Baggins Jan 30 '13 at 11:04
  • Oh and please... title your post carefully. My first comment answers precisely that. – Wilbo Baggins Jan 30 '13 at 11:11
  • Now is possible, [Check this link](https://developer.apple.com/documentation/foundation/nsnotification.name/1614835-nsbundleresourcerequestlowdisksp) – Juan Boero May 11 '18 at 15:40

3 Answers3

7

Slightly off-topic ... but feeling compelled to write this one.

Why are notifications about disk space changes bad ?

Think about it. There are multiple issues which make knowing "available" or "used" space a rather strongly ad-hoc / point-in-time snapshot, and therefore much more suitable for being queried than being notified upon.

Filesystems (and/or a databases) are abstraction layers on top of "disks" (let's call "storage devices", for the purpose of this posting, "disks"). That means, in particular, that assumptions like:

  • Used disk space is equal (or even close) to the sum of the sizes of all files
  • Free disk space can be allocated completely to a newly-created file, or an existing file could be grown by that amount
  • a size change (growth or truncation) of a file would be reflected in an equal change of free/used disk space
  • Free disk space is an exact / exactly knowable quantity
  • Total disk space is constant

are incorrect.

That's because the filesystem is free to implement techniques like:

  1. Compression - makes used space less than the sum of file sizes, and makes exact free space unknowable because compression ratios of future data to be written can't be predicted
  2. Deduplication - similar effect (on used/free disk space)
  3. Snapshots - makes the filesystem retain "deleted" data so disk space isn't "freed" even though you think you released space previously used by your files
  4. Sparse files - old technique, could call it "zeroes compression", i.e. don't allocate disk space if all that's written are zeroes
  5. Space reservations - the filesystem may set aside a certain amount of space for use only by privileged apps/users, and/or only to be used through specific interfaces
  6. Online filesystem resizing - might grow/shrink the total amount of disk available to the filesystem
  7. Live defragmentation - might coalesce used space
  8. Block redistribution - for flash devices, might lead to coalescing on erase block level and allow the flash FTL to free space
  9. Journal rolling - might either free up or increase usage depending on the types of transactions in the log
  10. ... and what not.

Also, while file sizes are byte-accurate, disk allocation is managed in larger-sized quantities (blocks / sectors / stripes / ...).

So a filesystem can present a completely static (from the point of view what files and directories are visible and what they contain) view to the application world while e.g. running an online deduplication/compression/defragmentation/... job as background activity that'll keep changing "disk space usage" statistics as fast as storage and/or CPU power allows.

Modern filesystems (many that run on flash-based storage, for example) often implement a significant subset of the above techniques, and therefore would, were they to implement an interface that'll notify for every change in "allocatable disk space", not only report possibly meaningless data, but also incur a significant performance penalty by virtue of their "normal" operation possibly inducing a huge number of such notifications.

Ergo:

Do not expect to get notified. Poll for this information - explicitly query for it, at reasonable intervals. Don't to get CPU-bound, particularly not on mobile devices, and expect "surprising" results, i.e. changes without any apparent user action as well as no changes where user action has happened. Take the data you retrieve with a grain of salt and don't draw too many conclusions from it. That's modern filesystems for you ...

FrankH.
  • 16,133
  • 2
  • 36
  • 54
1

No, there isn't any notification posted by the system.

But luckily, you can monitor the size of remaining free space yourself and do something when you notice a change.

Check out How to detect total available/free disk space on the iPhone/iPad device? to see how.

Community
  • 1
  • 1
Wilbo Baggins
  • 2,592
  • 2
  • 24
  • 37
0

You can refer this link here How to detect total available/free disk space on the iPhone/iPad device?

You can try this.

-(unsigned)getFreeDiskspacePrivate {
    NSDictionary *atDict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/" error:NULL];
    unsigned freeSpace = [[atDict objectForKey:NSFileSystemFreeSize] unsignedIntValue];
    NSLog(@"%s - Free Diskspace: %u bytes - %u MiB", __PRETTY_FUNCTION__, freeSpace, (freeSpace/1024)/1024);

    return freeSpace;
}

and this also

-(uint64_t)getFreeDiskspace {
    uint64_t totalSpace = 0.0f;
    uint64_t totalFreeSpace = 0.0f;
    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 floatValue];
        totalFreeSpace = [freeFileSystemSizeInBytes floatValue];
        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 = %@", [error domain], [error code]);  
    }  

    return totalFreeSpace;
}
Community
  • 1
  • 1
Saurabh Shukla
  • 1,346
  • 3
  • 14
  • 26