问题
In my app, I have a SyncAdapter
which uses a ContentProvider
to put data into the SQLiteDatabase. Everything was working perfectly for months. After I have added a second way to update the ContentProvider from GCM push notifications, I start getting crash reports from some users like
android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 3850)
OR
android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 778)
and several other exceptions related to sqlite.
Although I've heard that Sqlite is synchronized and we don't have to worry, I do get these errors that point that I need to synchronize somehow. Has anyone dealt with this issue?
EDIT: Here's a sample stacktrace
android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 778)
1
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
2
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:906)
3
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
4
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
5
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
6
at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
7
at com.makanstudios.conscious.provider.ConsciousProvider.doInsert(ConsciousProvider.java:225)
8
at com.kaciula.utils.provider.BasicContentProvider.insert(BasicContentProvider.java:80)
9
at android.content.ContentProvider$Transport.insert(ContentProvider.java:201)
10
at android.content.ContentResolver.insert(ContentResolver.java:866)
11
at com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:351)
12
at com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:362)
13
at com.makanstudios.conscious.gcm.GCMIntentService.onMessage(GCMIntentService.java:124)
14
at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
15
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
16
at android.os.Handler.dispatchMessage(Handler.java:99)
17
at android.os.Looper.loop(Looper.java:137)
18
at android.os.HandlerThread.run(HandlerThread.java:60)
And another sample stacktrace:
0
android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
1
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
2
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:318)
3
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:229)
4
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
5
at android.database.sqlite.SQLiteConnectionPool.reconfigure(SQLiteConnectionPool.java:339)
6
at android.database.sqlite.SQLiteDatabase.reopenReadWrite(SQLiteDatabase.java:832)
7
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:213)
8
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
9
at com.kaciula.utils.provider.BasicContentProvider.insert(BasicContentProvider.java:76)
10
at android.content.ContentProvider$Transport.insert(ContentProvider.java:201)
11
at android.content.ContentResolver.insert(ContentResolver.java:869)
12
at com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:351)
13
at com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:362)
14
at com.makanstudios.conscious.gcm.GCMIntentService.onMessage(GCMIntentService.java:124)
15
at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
16
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
17
at android.os.Handler.dispatchMessage(Handler.java:99)
18
at android.os.Looper.loop(Looper.java:137)
19
at android.os.HandlerThread.run(HandlerThread.java:60)
回答1:
Just use a single Helper inside your content provider code. So always connect 1 helper to 1 database and you won't have any issues.
As an alternative if you don't provide access to other applications there is no need to use a content provider.
I have been using content providers for a long time before I decided to switch to ORMLite. And it has been working great so far in all my projects. Maybe check that out.
来源:https://stackoverflow.com/questions/16610709/synchronize-access-to-content-provider