同步访问内容提供商 [英] Synchronize access to Content Provider
问题描述
在我的应用程序,我有一个 SyncAdapter
它采用了的ContentProvider
来把数据放到SQLiteDatabase。一切都完美的工作了几个月。之后,我已经添加到从GCM推送通知更新ContentProvider的第二种方式,我开始得到一些用户的崩溃报告就像
android.database.sqlite.SQLiteDiskIOException:磁盘I / O错误(code 3850)
或
android.database.sqlite.SQLiteDiskIOException:磁盘I / O错误(code 778)
和相关sqlite的其他几个例外情况。
虽然我听说SQLite是同步的,我们不担心,我得到这些错误指向,我需要以某种方式同步。有没有人处理这个问题?
编辑:
下面是一个示例堆栈跟踪
android.database.sqlite.SQLiteDiskIOException:磁盘I / O错误(code 778)
1
在android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(本机方法)
2
在android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:906)
3
在android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
4
在android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
五
在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
6
在android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
7
在com.makanstudios.conscious.provider.ConsciousProvider.doInsert(ConsciousProvider.java:225)
8
在com.kaciula.utils.provider.BasicContentProvider.insert(BasicContentProvider.java:80)
9
在android.content.ContentProvider $ Transport.insert(ContentProvider.java:201)
10
在android.content.ContentResolver.insert(ContentResolver.java:866)
11
在com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:351)
12
在com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:362)
13
在com.makanstudios.conscious.gcm.GCMIntentService.onMessage(GCMIntentService.java:124)
14
在com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
15
在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)
16
在android.os.Handler.dispatchMessage(Handler.java:99)
17
在android.os.Looper.loop(Looper.java:137)
18
在android.os.HandlerThread.run(HandlerThread.java:60)
和另一个样本堆栈跟踪:
0
android.database.sqlite.SQLiteException:不是一个错误(code 0):无法打开在读/写模式的数据库。
1
在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)
2
在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:318)
3
在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:229)
4
在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
五
在android.database.sqlite.SQLiteConnectionPool.reconfigure(SQLiteConnectionPool.java:339)
6
在android.database.sqlite.SQLiteDatabase.reopenReadWrite(SQLiteDatabase.java:832)
7
在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:213)
8
在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
9
在com.kaciula.utils.provider.BasicContentProvider.insert(BasicContentProvider.java:76)
10
在android.content.ContentProvider $ Transport.insert(ContentProvider.java:201)
11
在android.content.ContentResolver.insert(ContentResolver.java:869)
12
在com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:351)
13
在com.makanstudios.conscious.net.DatabaseHandler.updateStatsCount(DatabaseHandler.java:362)
14
在com.makanstudios.conscious.gcm.GCMIntentService.onMessage(GCMIntentService.java:124)
15
在com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
16
在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)
17
在android.os.Handler.dispatchMessage(Handler.java:99)
18
在android.os.Looper.loop(Looper.java:137)
19
在android.os.HandlerThread.run(HandlerThread.java:60)
只需使用您的内容提供商code里面的一个助手。因此,始终连接1帮手1数据库,你不会有任何问题。
作为替代方案,如果你不提供访问其他应用程序没有必要使用内容提供商
我一直在使用的内容提供商了很久我才决定改用ORMLite。它一直伟大的工作在我所有的项目至今。或许检查了这一点。
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)
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.
这篇关于同步访问内容提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!