The question is : When does GreenDao update the sql database from the session cache?
Let say that I'm querying an existing entity out of the database and than change one of it properties(fields). The changes are performed without directing the sql database in anyway.
So - should I do update on the entity?
When does GreenDao flush it's session cache into the database itself?
What you describe sounds like JPA/Hibernate session cache. greenDAO doesn't have that. It is simpler. There are objects kept in memory and you always update them manually to the database.
Some more information (far from complete):
http://greendao-orm.com/documentation/sessions/
When one generates the GreenDAO code (Entities, DAOs, DaoSession....etc), the DAOs by default extend the AbstractDao class which has a variety of methods for updating the database when needed instead of keeping the update in memory for a while such as (update(T entity), refresh(T entity), updateInTrx(java.lang.Iterable entities)).
For more information check the AbstractDao javaDoc
Related
What are caveats of using enableMultiInstanceInvalidation. What all checks i should make when using this construct. Can i update my database builder if database is already created in the storage.
MultiInstanceInvalidationClient is a non trivial amount of code and work that needs to happen when typically a DB is for a simple app/process.
https://androidx.tech/artifacts/room/room-runtime/2.1.0-alpha05-source/androidx/room/MultiInstanceInvalidationClient.java.html
I’m new to Android development and I’m about to implement simple Preferences for my app. It appears SharedPreferences is a dead end and has lots of warts, so I’m looking at DataStore (non-Proto) vs Room. Since I ALREADY heavily use Room and LiveData (yes, I know Flow is the new hotness) in my app for other things, is there any benefit to using DataStore too? I understand Room is recommended for large or complex data as I’ve already reviewed the following, but I’m hoping a more seasoned developer can further hit this home for me:
https://android-developers.googleblog.com/2020/09/prefer-storing-data-with-jetpack.html
https://proandroiddev.com/lets-explore-jetpack-datastore-in-android-621f3564b57
https://medium.com/better-programming/jetpack-datastore-improved-data-storage-system-adec129b6e48
Thank you.
The official blog post you linked has a section specifically about Room vs DataStore:
If you have a need for partial updates, referential integrity, or support for large/complex datasets, you should consider using Room instead of DataStore. DataStore is ideal for small , simple datasets and does not support partial updates or referential integrity.
User preferences almost always fall into the 'small, simple datasets' that can be easily expressed as key/value pairs (or something more complicated if you want to use the Proto DataStore) that do not need the overhead of a table schema, SQL queries, custom parsing, or the 'relational' part of a relational database.
The problem with datastore is you cannot just fetch or update a part of data from a list like you can with SQLite libraries such as Room. This is true for both Proto and Preferences version. So if you have 10 thousand elements and you save them to DataStore and then you want to update 2 of them based on a condition you'll have to fetch the entire list, manipulate it and put it back. Here Room (or any DB solution) will be a way to go
But if you just want to save user preferences or small data it would be an overkill to use a DataBase - here DataBase Proto will actually be the perfect choice
I am looking at ways to migrate a SQLite DB in an android app to Realm. The DB has about 2000 records that need to be inserted upon first load. Is there a way to do this using the migration mechanism (https://realm.io/docs/java/latest/#migrations) and createOrUpdateAllFromJson()?
Migrations are not really used to insert data on first load. RealmConfiguration has a method called initialData() that is much more suited for this: https://realm.io/docs/java/latest/api/io/realm/RealmConfiguration.Builder.html#initialData-io.realm.Realm.Transaction-
createOrUpdateAllFromJson() only makes sense if you can export you SQLite database to JSON. Most likely it will be much faster to just read the data directly from SQLite and insert them into Realm.
A more type-safe method would be to use copyToRealmOrUpdate() if you can somehow export your SQLite data to a in-memory object representation.
I'm trying to figure out the best way to handle database upgrades and versioning.
At the moment I delete the database and log users out when I do a point release, which isn't a great experience.
Can anyone recommend any tips for doing this?
Your database version is independent of your app version. If your database schema doesn't change at all, you shouldn't need to do anything to your database during an update.
When your database schema changes, you should handle database updates in onUpgrade() of your SQLiteOpenHelper. This method is called when you try to access your database and you have updated your database version, as described in the Data Storage Options documentation.
If you are using a third party library to handle your databases, it should either handle the upgrade for you or provide similar functionality.
There is no universal strategy for upgrading your database here. What you do depends completely on what your schema looked like before the upgrade and what the new schema looks like. Depending on what changed, you might create new tables or columns, delete tables or columns, update rows in the database, or move data between tables. If you have a specific question about how to migrate your data, create a new question describing the new and old schemas.
The way we do it is that we run a routine every time the app starts that calls a stored proc on the server to get SQL that upgrades the database if it is necessary. (The sql can be quite involved: dropping tables and recreates them with new structures and inserting new values). We store the version of the database in the database itself and upgrades to the new version.
We don't use the onUpgrade() call.
How can I insert a list of data into SQLite which is fetched from a remote database? Also I want to list out these data during offline mode. What is the easiest method to do this?
You can create a ContentProvider (in most cases it ends up necessary)
http://developer.android.com/guide/topics/providers/content-provider-basics.html
Or use the SQLiteDatabase and SQLiteOpenHelper classes:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Or the ORMLite library:
http://ormlite.com/sqlite_java_android_orm.shtml
the best way is, i have the same thing as you. from an api i will get thousands of xml and need it to store in 3 tables of a database. it will happen when i launch the app. for the best practice i will do the fetching data from api and storing it in 3 tables in Async Task as follows:
do in background(){
do your fetching data from remote
store in db.
}