KISS

Keep It Simple Stupid

How to save and restore application data on iOS Simulator quickly

| comments

I had a post a long time ago describing the usage of directory hardlinks in order to easily access, backup and restore the container of your application on the iOS Simulator: Persistent application container directories in iOS Simulator. Obviously now it’s an APFS era (enforced by apple) and I’m not sure if it has directory hardlinks anymore. So I have another, simpler way to backup app containers.

Thanks to simctl, it’s now very easy to find the container directory of your program in any iOS Simulator. I typically work with only one and simctl understands the string booted for the currently booted simulator.

To backup your application’s current data, you simply run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
~ $ rsync -av --delete "$( xcrun simctl get_app_container booted org.example.app data )/" ~/Desktop/123_incorrect_state/
sending incremental file list
created directory /Users/user/Desktop/123_incorrect_state
./
.com.apple.mobile_container_manager.metadata.plist
Documents/
Documents/data.sqlite
Documents/data.sqlite-shm
Documents/data.sqlite-wal
Documents/default.realm
Documents/default.realm.lock
Documents/default.realm.note
Documents/default.realm.management/
Documents/default.realm.management/access_control.control.mx
Documents/default.realm.management/access_control.new_commit.cv
Documents/default.realm.management/access_control.pick_writer.cv
Documents/default.realm.management/access_control.write.mx
Library/
Library/Application Support/
Library/Application Support/config.txt
Library/Caches/
Library/Caches/debugLoggingInitialRunCheck.txt
Library/Caches/Logs/
Library/Caches/Logs/org.example.app-2021-06-01--18-03-47-493.log
Library/Caches/com.apple.WebKit.Networking/
Library/Caches/com.apple.WebKit.WebContent/
Library/Caches/com.apple.nsurlsessiond/
Library/Caches/com.apple.nsurlsessiond/Downloads/
Library/Caches/com.apple.nsurlsessiond/Downloads/org.example.app/
SystemData/
tmp/
tmp/org.example.app/
tmp/org.example.app/WebKit/
tmp/org.example.app/WebKit/MediaCache/

sent 13,560,443 bytes  received 2,602 bytes  27,126,090.00 bytes/sec
total size is 13,543,239  speedup is 1.00

In the command, org.example.app is the application’s bundle ID, and the last argument is where to store the backup. Remember to include the trailing slashes on both the source and destination directories.

And to restore back, you need to make sure the application is installed on the booted simulator (or install a fresh one and quit it) and then swap the source and destination:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~ $ rsync -av --delete ~/Desktop/123_incorrect_state/ "$( xcrun simctl get_app_container booted org.example.app data )/"                                                                                                  <<<
sending incremental file list
deleting Library/Caches/Logs/org.example.app-2021-06-02--13-06-21-461.log
deleting Library/SplashBoard/Snapshots/sceneID:org.example.app-default/D5D9F92F-D6B9-4874-B679-D0182B245CE6@2x.ktx
deleting Library/SplashBoard/Snapshots/sceneID:org.example.app-default/downscaled/1020E7B3-AD27-4725-BB90-0BD7DF936864@2x.ktx
Documents/data.sqlite
Documents/data.sqlite-shm
Documents/data.sqlite-wal
Documents/default.realm
Documents/default.realm.lock
Documents/default.realm.management/access_control.control.mx
Documents/default.realm.management/access_control.write.mx
Library/Caches/Logs/
Library/Caches/Logs/org.example.app-2021-06-01--18-03-47-493.log
Library/Preferences/
Library/Preferences/org.example.app.plist
Library/SplashBoard/Snapshots/sceneID:org.example.app-default/
Library/SplashBoard/Snapshots/sceneID:org.example.app-default/10D752DA-DBC3-4DCF-B024-4EFC8E3F6C9D@2x.ktx
Library/SplashBoard/Snapshots/sceneID:org.example.app-default/downscaled/
Library/SplashBoard/Snapshots/sceneID:org.example.app-default/downscaled/0FA4DB59-0F12-4A36-AB6C-0F3B6AA28892@2x.ktx
tmp/

sent 6,161,528 bytes  received 1,991 bytes  12,327,038.00 bytes/sec
total size is 13,543,239  speedup is 2.20

As you see, the --delete flag removes any files in the app container which are not in the saved backup.

These are easy commands that work very fast and help me have a productive development environment where I can restore a known application state again and again.

Comments