Architecture Components - Persistence and Offline (Google I/O '17)

By: Android Developers

236   5   15034

Uploaded on 05/18/2017

Writing database-driven apps on Android requires too much boilerplate code and too many edge cases that must be handled manually. This session will cover a new approach that allows you to architect your persistence layer in a much more robust and scalable way, benefiting from the full power of SQL without sacrificing compile-time code guarantees. We'll also discuss approaches and features that make offline persistence easier. Be sure to also check out the other two “Architecture Components” sessions for more information on architecting better Android applications.

Watch more Android talks at I/O '17 here:
See all the talks from Google I/O '17 here:

Subscribe to the Android channel:

#io17 #GoogleIO #GoogleIO2017

Comments (5):

By anonymous    2017-09-20

The answer below is correct, but I believe that this Answer and Question will soon be "deprecated" along with most third-part ORMs. Google's new Room Persistence Library (Yigit's Talk) will be preferred in most cases. Although DBFlow will certainly carry on (Thank You Andrew) in many projects, here is a good place to re-direct people to the newest "best practice" because this particular question was/is geared for those new to DBFlow.

The correct way to initialize the database (akin to the SQLiteOpenHelper's onCreate(db) callback is to create a Migration object that extends BaseMigration with the version=0, then add the following to the onCreate() in the Application class (or wherever you are doing the DBFlow initialization):

FlowManager.init(new FlowConfig.Builder(this).build());

In the Migration Class, you override the migrate() and then you can use the Transaction manager to initialize lookup data or other initial database content.

Migration Class:

@Migration(version = 0, database = BracketsDatabase.class)
public class DatabaseInit extends BaseMigration {
    private static final String TAG = "classTag";

public void migrate(DatabaseWrapper database) {
    Log.d(TAG, "Init Data...");
    Log.d(TAG, "Data Initialized");

To populate the data, use your models to create the records and the Transaction Manager to save the models via FlowManager.getDatabase(AppDatabase.class).getTransactionManager() .getSaveQueue().addAll(models);

Original Thread

By anonymous    2017-09-20

If your JSON tree structure will not change often, you can make a PoJo for it with a tool such as jsonschema2pojo. And convert instances using Gson.

You can store this PoJo using Google's new library: Room Persistence Library.

It can save the PoJo for you (You'll need to annotate it with @Entity) in sqlite database, and it can update you on changes in the database. For more details you can watch the Google I/O presentations.

Original Thread

By anonymous    2018-01-22

Yes "nested embedded objects" are permitted inside ROOM. You can write a User class which has an embedded Address class as which contains a embedded Location class.

Each time when an embedded object is added, room flattens out the table. In your case room generates a table called "User" with the following columns:

id, firstName, addr_street, addr_state, addr_city, addr_post_code, addr_home_lat, addr_home_lng, addr_office_lat, addr_office_lng

So your query should be like :

@Query("SELECT * FROM User WHERE " +
        "addr_home_lat BETWEEN :lat1 AND :lat2" +
        " AND addr_home_lng BETWEEN :lng1 AND :lng2")
List<User> findInRange(long lat1, long lat2, long lng1, long lng2);

Notice that "lat" has been flattened to "addr_home_lat" and "lng" to "addr_home_lng". So you can use these column names for applying the filter logic.

In case you misspelled a column name for example "home_lng" instead of "addr_home_lng", then room will find that out and give you an error :

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such column: home_lng)

For more information on room database, check out the Google I/O talk.

Original Thread

Submit Your Video

If you have some great dev videos to share, please fill out this form.