BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 8: Storage

Cascades

Part 8 of our Porting Series has us focus on Storage.

The Cascades Framework uses a modular approach to store, access, and display data in your apps.

There is a good chance you will need your app to interact with external data – the raw data for your app. The format of the external data can vary depending upon your needs and Cascades provides classes that help you manage three common data formats: XML, JSON, and SQL.

First, let’s look at a table comparing BBOS and BlackBerry 10 Data Storage:

Storage Type BlackBerry OS BlackBerry 10
Application Storage
(internal to the BlackBerry deice)
Contains the operating system, Java® Virtual Memory (JVM), and an internal file system. Application storage is also called flash memory and onboard memory. Application storage is the only place on a BlackBerry device from which applications can be run. All BlackBerry devices have application storage. Three file systems exist, BASE, USER and RADIO. Please note that BASE and RADIO are both “read-only”.
USER is read/write and contains the applications and BlackBerry 10 platform volatile data.
External Media Card Storage
Optional and removable microSD card that Users can insert to extend the storage on their device.
A File Allocation Table (FAT) file system is mounted on the media card. microSD cards are supported on all devices running BlackBerry® Device Software 4.2 or later. A File Allocation Table (FAT) file system is mounted on the media card.

USER File system

BlackBerry 10 introduces a file system (recall on BBOS there was no file system). So, what to know about the file system? Firstly, the working directory is where your app is started – it is also called current directory and the sandbox ($sandbox/app). Access to files and folders in the working directory is controlled through Unix-style groups and permissions. Key point: never hardcode absolute file paths – all should be relative to your sandbox.

Please review the following file system diagram:

And visit this page :

https://developer.blackberry.com/cascades/documentation/device_platform/filesystem/index.html

For more information on the file system.

Secondly, let’s look at working with XML, JSON, and SQL. The following provides some code snippets for all three along with links to more information.

XML

The following is a code snippet on how to load XML using a GroupDataModel

// Create the data model, specifying sorting keys of "firstName" and "lastName" 
GroupDataModel *model = new GroupDataModel(QStringList() << "firstName"
                                           << "lastName"); 

// Create an XmlDataAccess object and load the .xml file. The QDir::currentPath() 
// function returns the current working directory for the app. 
XmlDataAccess xda; 
QVariant list = xda.load(QDir::currentPath() + 
                         "/app/native/assets/employees.xml", 
                         "/employees/employee");

For more on XML data, please click “Working with XML”

http://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_xml.html

JSON

The following is a code snippet on how to create a JSON Data Access Object and load the JSON file

// Create a JsonDataAccess object and load the .json file. The  
// QDir::currentPath() function returns the current working  
// directory for the app. 
JsonDataAccess jda; 
QVariant list = jda.load(QDir::currentPath() + 
                         "/app/native/assets/employees.json");

For more on JSON, please click “Working with JSON data”

http://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_json.html

SQL

The following is a code snippet on how to create SQL

 // Create an SqlDataAccess object 
    SqlDataAccess sda(home.absoluteFilePath("mySQLFile.db")); 

    // Create a table called Employee in the database file 
    sda.execute("CREATE TABLE Employee( firstName VARCHAR(50), 
                 lastName VARCHAR(50), salary INT);"); 

    // Insert employee records into the table 
    sda.execute("INSERT INTO Employee (firstName, lastName, salary) 
                 VALUES ("Mike", "Smith", 42000);"); 
    sda.execute("INSERT INTO Employee (firstName, lastName, salary) 
                 VALUES ("Joe", "Brown", 55000);");

For more on SQL data, please click “Working with SQL”

http://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_sql.html

In conclusion, the data files that you work with in your apps are stored in the file system on the device. Different file system locations have different permissions; in some areas, you can create new files to store your app data, while in others, you can only load data from existing files. If you want to access your data files using the method that works best for you, you need to understand where your data files are located in the file system. Please follow this link “File system access for app” for more information.

Data files that you package with your app (when you compile the app) are located in the assets folder in the file system. Your app has read-only access to this folder, which means that you can load data from files in this location, but you can’t save new data in these files. When you create new data files dynamically while your app is running, they are stored in the data folder. Your app has full read-write access to this folder, so you can load and save data using files in this location.

Now on to Part 9!!

About Alex Kinsella

Promoter of apps. Connoisseur of tacos. By day, I'm a PR and Social Media Manager at BlackBerry.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus