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: https://goo.gl/c0LWYl
See all the talks from Google I/O '17 here: https://goo.gl/D0D4VE

Subscribe to the Android channel: http://goo.gl/GEh1ds

#io17 #GoogleIO #GoogleIO2017

Comments (4):

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());
FlowManager.getDatabase(BracketsDatabase.NAME).getWritableDatabase();

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";

@Override
public void migrate(DatabaseWrapper database) {
    Log.d(TAG, "Init Data...");
    populateMethodOne();
    populateMethodTwo();
    populateMethodThree();
    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

Recommended Books

    Popular Videos 56

    Submit Your Video

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