Proper Way To Access Db From Both Activity And A Started Service?
Short version: what is the best practice way to access the same DB from both activity and from started service? Long version: I have a case where I start persistent service from ac
Solution 1:
The usual paradigm for any database on Android is to have one provider open the database and handle requests as intents. This has the advantage of serializing requests.
Opening databases from different code is completely non-standard.
If you really need an app to notice each time something is added, a broadcast intent from the provider might be the answer.
Solution 2:
I have solved the problem using retain/release counter:
publicclassDBHandlerextendsSQLiteOpenHelper
{
privatestaticintretainCount=0;
@Overridepublicsynchronized SQLiteDatabase getReadableDatabase()
{
DBHandler.retainCount++;
returnsuper.getReadableDatabase();
}
@Overridepublicsynchronized SQLiteDatabase getWritableDatabase()
{
DBHandler.retainCount++;
returnsuper.getWritableDatabase();
}
@Overridepublicsynchronizedvoidclose()
{
DBHandler.retainCount--;
if (DBHandler.retainCount == 0)
super.close();
}
}
}
Post a Comment for "Proper Way To Access Db From Both Activity And A Started Service?"