Move files into a writable directory

You can have your program move files into a read-write (writable) directory.

If you create a flat file to hold all the files required by the app, a flat file is created on the mobile device. This is done by the default packaging nodes when you create a new BAM Mobile Project (.4pw).

Warning: Take care that you do not use the same file name for a read-only resource and a read-write resource. Using the same name for both can lead to problems.

Androidâ„¢

For your Android applications, this works out-of-the-box. After the package is deployed to the device, it is unpacked into a single directory on the device. This is a read-write directory, which means that any file that need to be writable (such as a database file) can be updated by the app.
Warning: If you redeploy an Android application, ALL files are overwritten, to include files such as a database file. You must take this into account as you plan your application upgrades, and handle any upgrade strategy in your app.

iOS

For your iOS applications, the directory created and holding the app files is a read-only directory. If writable files (such as a database file) need to be updated by the app, those files must be placed into a read-write directory on the device. Moving files from the read-only directory to a read-write directory is not something handled by packaging. You must handle it within your app.

You are provided with two APIs that allow you to reference the underlying directories transparently:
  • The base.Application.getProgramDir() method returns the base program directory, storing your compiled files, an initial database file, and so on. On an iOS device, this is a read-only directory.
  • os.Path.pwd() defines a writable directory for holding writable files, such as an error log or the user database.

At deployment, when your application initially starts, we recommend that you copy the writable files from the application directory (using the function base.Application.getProgramDir()) to the current working directory (using the function os.Path.pwd()).

For an example, open the OfficeStoreMobile demo project, open the OrdersApp.4gl intermediate file, and look for the OrdersApp_install function. In this function, the os.Path.copy method is used to copy a database file from the read-only source directory to the read-write destination directory.

Warning: The example provided by the OrdersApp.4gl does NOT cover an upgrade strategy. If you are upgrading, see Manage App updates.