简体   繁体   中英

iOS - NSFIleManager - How to deal with low disk space

When writing data to file (eg Thumbnails for caching, user data, etc.), how do you deal with the fact that the iDevice could not be able to write your data to file since the disk is full?

Will NSFileManager throw an exception in case of low disk space?

What's the designated way to deal with this and to inform my user that there's very little disk space left for his data? (I'm saving a fair amount of different data at different places in my app and searching for a common way to deal with it.)

As you mentioned in the comments that you want to save NSDictionary . If you only want to know whether the file is saved successfully or not, you can inspect the return value of the writeToFile:atomically: function.

Return Value
YES if the file is written successfully, otherwise NO.

More information under the NSDictionary 's Storing Dictionaries Section .

Alternatively,

If you want to get a more detail error message for the failure (such as out of disk space, folder not exist and etc.), then you can convert the NSDictionary to NSData before saving it .

  • NSDictionary to NSData :

     NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; 
  • NSData to NSDictionary :

     NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData]; 

The benifits is that you will also have access to this API -writeToFile:options:error: .

If there is an error writing out the data, upon return contains an NSError object that describes the problem.

Also more detail could be found under the Storing Data Section of NSData .

I think that's the best you can do in case there is a low disk space problem on the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM