我有Android应用程序与服务器对话并在SQLite数据库中同步一些数据。有一个服务每5分钟运行一次,跨越AsyncTask,可以抓取数据并将数据插入/更新到数据库中。
这是我的问题。当后台进程更新数据库时,可能需要相当长的时间。如果用户同时尝试使用应用程序并打开ListView并将游标绑定到同一个表 - 我会遇到死锁情况和ANR报告。
我该如何妥善解决这个问题?
我觉得我需要从UI检查asyncprocess是否正在运行(如何?),如果是,则表示用户正在进行后台刷新并在进程访问数据完成后立即加载列表。
解决此问题的正确方法是什么?
修改
这是报告
DALVIK THREADS:
"main" prio=5 tid=1 WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x2aac88b8 self=0xcd58
| sysTid=3967 nice=0 sched=0/0 cgrp=unknown handle=1876207664
at java.lang.Object.wait(Native Method)
- waiting on <0x2aac8948> (a java.lang.VMThread)
at java.lang.Thread.parkFor(Thread.java:1535)
at java.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
at sun.misc.Unsafe.park(Unsafe.java:317)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:131)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:790)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:823)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1153)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:200)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:375)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:61)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
at android.content.ContentResolver.query(ContentResolver.java:251)
****************at com.idatt.data.PreferenceData.getPreferenceString(PreferenceData.java:119)
at com.idatt.data.PreferenceData.getPreferenceBoolean(PreferenceData.java:109)
at com.idatt.common.Preferences.getIsShowRates(Preferences.java:136)
at com.idatt.activities.TripListActivity$TripViewDataHolder.populateFrom(TripListActivity.java:211)
at com.idatt.activities.TripListActivity$TripListCursorAdapter.getView(TripListActivity.java:147)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
at android.widget.ListView.onMeasure(ListView.java:1109)
at android.view.View.measure(View.java:8187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3146)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3146)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3146)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8187)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3146)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8187)
at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4644)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #3" prio=5 tid=14 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x3033f218 self=0x2394d0
| sysTid=4305 nice=0 sched=0/0 cgrp=unknown handle=2332488
at dalvik.system.NativeStart.run(Native Method)
"AsyncTask #5" prio=5 tid=13 WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x300dc038 self=0x25f0e0
| sysTid=4073 nice=10 sched=0/0 cgrp=unknown handle=2965040
at java.lang.Object.wait(Native Method)
- waiting on <0x3013c610> (a java.lang.VMThread)
at java.lang.Thread.parkFor(Thread.java:1535)
at java.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
at sun.misc.Unsafe.park(Unsafe.java:317)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:131)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:790)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:823)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1153)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:200)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:375)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1533)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
at com.idatt.data.Provider.insert(Provider.java:198)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
at android.content.ContentResolver.insert(ContentResolver.java:587)
*************at com.idatt.data.TripData.InsertTrip(TripData.java:272)
at com.idatt.common.AsyncProcessor.GetUserTrips(AsyncProcessor.java:252)
at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:101)
at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:27)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
"AsyncTask #4" prio=5 tid=12 WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x302ffd28 self=0x4e8f30
| sysTid=4072 nice=10 sched=0/0 cgrp=unknown handle=5163584
at java.lang.Object.wait(Native Method)
- waiting on <0x302ffbd8> (a java.lang.VMThread)
at java.lang.Thread.parkFor(Thread.java:1535)
at java.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
at sun.misc.Unsafe.park(Unsafe.java:317)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:131)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:790)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:823)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1153)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:200)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:375)
at android.database.sqlite.SQLiteProgram.close(SQLiteProgram.java:291)
at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:133)
at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:502)
at android.database.CursorWrapper.close(CursorWrapper.java:45)
at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1355)
************at com.idatt.data.LogData.getLogItems(LogData.java:118)
at com.idatt.data.LogData.getLogItems(LogData.java:125)
at com.idatt.common.AsyncProcessor.PostDeviceLogs(AsyncProcessor.java:210)
at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:96)
at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:27)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
"AsyncTask #3" prio=5 tid=11 WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x302ec858 self=0x4ff440
| sysTid=4071 nice=10 sched=0/0 cgrp=unknown handle=2984848
at java.lang.Object.wait(Native Method)
- waiting on <0x302ec9b0> (a java.lang.VMThread)
at java.lang.Thread.parkFor(Thread.java:1535)
at java.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
at sun.misc.Unsafe.park(Unsafe.java:317)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:131)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1996)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:359)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1001)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1061)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
"AsyncTask #2" prio=5 tid=10 WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x302c31a8 self=0x4dca78
| sysTid=4062 nice=10 sched=0/0 cgrp=unknown handle=5111608
at java.lang.Object.wait(Native Method)
- waiting on <0x3027ab38> (a java.lang.VMThread)
at java.lang.Thread.parkFor(Thread.java:1535)
at java.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
at sun.misc.Unsafe.park(Unsafe.java:317)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:131)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:790)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:823)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1153)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:200)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
at android.database.sqlite.SQLiteDatabase....
见附加的痕迹。检查我用 ** 标记的行我读的不是很好但是这是我的结论..似乎它在尝试执行时阻塞了
com.idatt.data.PreferenceData.getPreferenceString
此调用以只读模式打开数据库(它通过我的COntentProvider)。这个调用发生在UI线程上。
有什么不好 - 我注意到其他两个电话: InsertTrips 和 getLogData 。 InsertTrips进入数据库,但getLogData也应该是只读的。但是,它们都是同一个AsyncTask的一部分。因此,似乎AsyncTask的一个实例源自服务/警报(每5分钟处理),第二个实例源自用户(我给他们选择手动刷新)。
情况看起来像这样:
答案 0 :(得分:1)
当您使用用于获取列表游标的表或因为您访问数据库而锁定时,它是否会锁定。如果lock是基于表的,你可以考虑有两个表,一个用于服务,一个用于视图,或者两个表,你切换到一个用于显示,并使用另一个表来填充数据......
如果lock是基于数据库而不是基于表的,那么我想到的另一个解决方案是,当您为表获取数据时,可以从数据库缓存数据,使用arrayadapter而不是游标适配器。查询数据库,填充缓存并显示它,以便服务使用数据库填充新值。服务终止后,您可以发送oberserupdate通知UI获取新数据并重新填充其缓存。
此致 斯特凡
答案 1 :(得分:0)
如果您正确使用AsyncTask,则不应阻止您的UI线程。没有代码,很难说这里出了什么问题。要检查的一些事项:
确保所有人都调用AsyncTask.execute(),现在调用AsyncTask.doInBackground()。直接调用doInBackground()将在UI线程上运行该代码。
您是否在更新期间锁定的某个对象上手动同步?