TwitterCopy link
Copied

File management in the iOS application

Hey, hello everyone 👋

Welcome to another post. Today I will talk about the organization of directories, files, and notices when working with them on the iOS application.

If you are familiar with the Apple ecosystem in general or iOS specifically, you may already know that users do not have direct access to the file system. When you develop an app that supports the process, persists data offline, and back up later, or more popular use-case is the editor app: you need to save the editing ones temporarily, reverse to original or delete after export successfully, etc. With all of those operations related to files like that, understanding the file organization inside the app is very useful to work with it, saving your time for debugging around, less consuming your user device’s space, and avoiding creating excessive memory pressure to the system. So let’s take a look at file management in the iOS application with me to see how it organize? What is its characteristic? Which folder should we put our files on? And much more.

Overview

As usual, before going deep into the detail, let’s look at the visualization first. I believe that having the structure and image in mind will help us easier comprehend the points later 😉

iOS file management.png

There are two main parts in the app sandbox created while installing a new app: bundle and data containers. Besides that, the app may also need additional container directories such as iCloud container, etc., but they’re another story, and it’s not my purpose for this post, so I skip it for now.

Okay, so what is it? What does it include? Or what is its role? Or can I put the data files under any directory I want to? 🤷‍♂️ From my experience, for the easier life and direct to the pain point of this fuzzy topic, I conclude them with these simple questions that we should care about:

  • Which directory’s data file expose to the user?
  • Which iTunes/iCloud back up the directory data file?
  • And how long the data in it can be live? (more precisely, Which directory’s data files can be deleted by the system?)

Let’s go through each to find out the answer to these questions:

  • Bundle container: containing your app and all of its resources.
  • Data container: the one I want to focus on in terms of the purpose of this post, the primary place for data of the app and user data files as well, we deal with it in most cases. There are further several subdirectories in there:
    • /Document: contains the user data file and is exposed to the user, backed up by iTunes or iCloud.
    • /Library: for any files which the user shouldn’t see, usually support files. It is backed up by iTunes or iCloud except the /Library/Caches directory.
    • /tmp: for temporary files used in the app’s current session, we should remove these files from this directory or move them to the suitable one when we are done with it because iTunes/iCloud doesn’t back it up, and also system may delete them.

My though

Generally, in my opinion, the /tmp directory is a suitable place which we are using for temporary data in a short period. Remember that the system may delete these data when the app is not running, so do not put the data between app sessions into this directory.

The /Library directory is for app-created support files, hidden away from the user sight. Especially, /Library/Caches is the place for the cached data we want to persist longer than in /temp directory. Still, the system may delete it at some point to free up disk space, so keep an eye on putting data files in this directory, preferably be the can be recreated or downloadable data, cached data to improve performance, etc.

The /document, a natural playground for you, data that the user can see can put here, images, videos, documents, etc. You can create subdirectories here for better organization. And the data in this one is also backed up. In other words, this directory is a safe choice for you to saving the user data file, but don’t abuse and mess it up, or your app takes up a lot of user’s space, the backup process takes too long to complete, and that contribute to the reason user deletes our app 🙂

That’s the main characteristics of each directory, and base on your purpose, your business logic you’re working on to decide where your user data file should be and organize them clearer.

Sharing

Last but not least, I found the great post here whole basic flow working with temporary files from create the directory, create a new file, write data to that file, and delete when done with it if needed or move to another place. Even the post mainly works with the temporary file, but you can totally base on the procedures and similarly APIs to apply for your own. It’s super clear and easy to understand.

Alright, that’s all from my understanding in terms of file management in the iOS application. I hope you’ve enjoyed the post. Thank you for staying with me, and have a great day 🤗


Dang Nguyen - d.f 2021