App file document directory changed when device language changes

Hi there,


My app is saving data to a Realm file on device. It supports multi-languages.


But now I found I'm facing a problem: whenever the language of the device or the simulator changes, the directory of app's document changes too.




Here is the example for simulator:

file:///Users/***/Library/Developer/CoreSimulator/Devices/DE161806-933E-4E99-983A-4CA2D9106CAC/data/Containers/Data/Application/059FD12F-433D-40BF-984C-4B08474182D4/Documents/aaa.realm


Here is the example from device:

var/mobile/Containers/Data/Application/F0A4F7C8-2319-4978-B600-EE1CDC38D3E1/Documents/main.realm


The underlined part changes each time when language of the device changes.


I also tried to download beta test version from TestFlight. Same installation, change the language of the device, the result is different. So I'm sure the URL of the Document changes every time the language changes.


The URL directory I got it as:

URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0], isDirectory: true)


Is there a way to keep the document directory same, regardless the language chose?


Thanks a lot!

Replies

Regardless of the specific circumstances under which this happens, the path to the your app’s container — and hence everything inside the container, including the Documents directory — can change. There’s no way to prevent this. Your app must be able to cope with this. For example, if you’re store a reference from one file to another, you should do one of the following:

  • Store a relative path

  • Store a path relative to the root of your container

  • Store an absolute path but be prepared to ‘fix’ that when the container changes path

  • Use a bookmark

Note that the system ensures that your app is not running before making such a change, so you don’t have to worry about it changing out from underneath you at runtime.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"