I need to recognize first launch of my application or activity.
At this time I need to get some information from server create local database and save info to it. What is the best way to do this?
- Create any preferences for example
FirstLaunchand settrue\falseto it. - Check whether my database exists or not.
- Something else?
PS. All server calls must be into one transaction. Ormlite supports transactions?
Thanks.
For the “create database at first run”-purpose, you should use an
SQLiteOpenHelper, which offers you theonCreate()-method that is called when:The Database-file itself will be created for you (you don’t have to do this manually). In this method, you can then perform actions like populating your database with standard entry’s.
If you want to populate the database with informations you get from your server, there might be a problem when there is no Internet-connection available.
In this case, I would check if there is a connection available:
To determine if your Database has be populated with the standard entry’s, you can use the database-version which is also provided by the
SQLiteDatabase-class:SQLiteOpenHelpers constructor and pass it0as the Databaseversion.
setVersion()-method to alter it to1.onOpen()-method, which is called when thedatabase is opened, you can check if the database was populated by
using the
getVersion()-method.Further more, the
getReadableDatabase()/getWritableDatabase()-methods should be called off the main-thread anyways because:So getting the informations from the Internet can take place in the
onCreate()and in theonOpen()-method (if it wasn’t successful at the first try). You can (for example) use a Service to do this.