我在使用安卓应用时遇到了问题。当应用程序试图保存ParseObject时,有时会阻塞UI并生成ANR。根据生成的traces.txt,只涉及我编写的一个方法,即saveClimbing。此方法仅调用ParseObject上的onSaveBackground:
public static void saveClimbing(final ParseObject p_climbing, final Climbing l_climbing){
p_climbing.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException ex) {
if(ex == null){
//no problems
l_climbing.setSaved(true);
ClimbApplication.climbingDao.update(l_climbing);
Log.i(getClass().getName(), "Climbing correctly saved in Parse");
}else{
l_climbing.setSaved(false);
ClimbApplication.climbingDao.update(l_climbing);
Log.e(getClass().getName(), ex.getMessage());
}
}
});
}
另一个涉及的方法是ParseUser.saveCurrentUserAsync,它会导致饥饿现象,但我不理解什么时候会调用它以及它为什么会导致这种情况。我做错了什么吗?
下面是traces.txt的第一部分:
JNI: CheckJNI is off; workarounds are off; pins=0; globals=180 (plus 1 weak)
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)
"main" prio=5 tid=1 MONITOR
| group="main" sCount=1 dsCount=0 obj=0x41700578 self=0x4009c120
| sysTid=26943 nice=0 sched=0/0 cgrp=apps handle=1074999292
| state=S schedstat=( 0 0 0 ) utm=3698 stm=442 core=0
at com.parse.ParseUser.getSessionToken(ParseUser.java:~439)
- waiting to lock <0x41fec980> (a java.lang.Object) held by tid=21 (Task.BACKGROUND_EXECUTOR-thread-47)
at com.parse.ParseUser.getCurrentSessionToken(ParseUser.java:963)
at com.parse.ParseObject.saveAsync(ParseObject.java:1348)
at com.parse.ParseObject$9.then(ParseObject.java:1337)
at com.parse.ParseObject$9.then(ParseObject.java:1333)
at com.parse.TaskQueue.enqueue(TaskQueue.java:58)
at com.parse.ParseObject.saveAsync(ParseObject.java:1333)
at com.parse.ParseObject.saveInBackground(ParseObject.java:1424)
at org.unipd.nbeghin.climbtheworld.util.ParseUtils.saveClimbing(ParseUtils.java:40)
at org.unipd.nbeghin.climbtheworld.ClimbActivity$8.done(ClimbActivity.java:1422)
at com.parse.FindCallback.internalDone(FindCallback.java:45)
at com.parse.FindCallback.internalDone(FindCallback.java:31)
at com.parse.Parse$6$1.run(Parse.java:917)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
"Task.BACKGROUND_EXECUTOR-thread-47" prio=5 tid=21 MONITOR
| group="main" sCount=1 dsCount=0 obj=0x4364af68 self=0x5b3d9cd0
| sysTid=28045 nice=0 sched=0/0 cgrp=apps handle=1597635584
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=1
at com.parse.ParseUser.saveCurrentUserAsync(ParseUser.java:~971)
- waiting to lock <0x41fd7df8> (a java.lang.Object) held by tid=1 (main)
at com.parse.ParseUser.access$000(ParseUser.java:20)
at com.parse.ParseUser$1.then(ParseUser.java:476)
at com.parse.ParseUser$1.then(ParseUser.java:469)
at com.parse.Task$11.run(Task.java:481)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeAfterTask(Task.java:477)
at com.parse.Task.continueWithTask(Task.java:353)
at com.parse.Task.continueWithTask(Task.java:364)
at com.parse.Task$9.then(Task.java:410)
at com.parse.Task$9.then(Task.java:402)
at com.parse.Task$11.run(Task.java:481)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeAfterTask(Task.java:477)
at com.parse.Task.access$400(Task.java:22)
at com.parse.Task$7.then(Task.java:346)
at com.parse.Task$7.then(Task.java:343)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.access$300(Task.java:22)
at com.parse.Task$6.then(Task.java:311)
at com.parse.Task$6.then(Task.java:308)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.access$300(Task.java:22)
at com.parse.Task$6.then(Task.java:311)
at com.parse.Task$6.then(Task.java:308)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.continueWith(Task.java:318)
at com.parse.Task.continueWith(Task.java:329)
at com.parse.Task$11.run(Task.java:485)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeAfterTask(Task.java:477)
at com.parse.Task.access$400(Task.java:22)
at com.parse.Task$7.then(Task.java:346)
at com.parse.Task$7.then(Task.java:343)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.ParseRequest$4.then(ParseRequest.java:324)
at com.parse.ParseRequest$4.then(ParseRequest.java:316)
at com.parse.Task$11.run(Task.java:481)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeAfterTask(Task.java:477)
at com.parse.Task.access$400(Task.java:22)
at com.parse.Task$7.then(Task.java:346)
at com.parse.Task$7.then(Task.java:343)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.continueWith(Task.java:318)
at com.parse.Task.continueWith(Task.java:329)
at com.parse.Task$11.run(Task.java:485)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeAfterTask(Task.java:477)
at com.parse.Task.access$400(Task.java:22)
at com.parse.Task$7.then(Task.java:346)
at com.parse.Task$7.then(Task.java:343)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.access$300(Task.java:22)
at com.parse.Task$6.then(Task.java:311)
at com.parse.Task$6.then(Task.java:308)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.access$300(Task.java:22)
at com.parse.Task$6.then(Task.java:311)
at com.parse.Task$6.then(Task.java:308)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.continueWith(Task.java:318)
at com.parse.Task.continueWith(Task.java:329)
at com.parse.Task$11.run(Task.java:485)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeAfterTask(Task.java:477)
at com.parse.Task.access$400(Task.java:22)
at com.parse.Task$7.then(Task.java:346)
at com.parse.Task$7.then(Task.java:343)
at com.parse.Task.runContinuations(Task.java:510)
at com.parse.Task.access$800(Task.java:22)
at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:565)
at com.parse.Task$TaskCompletionSource.setResult(Task.java:599)
at com.parse.Task$11$1.then(Task.java:493)
at com.parse.Task$11$1.then(Task.java:485)
at com.parse.Task$10.run(Task.java:448)
at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
at com.parse.Task.completeImmediately(Task.java:444)
at com.parse.Task.continueWith(Task.java:318)
at com.parse.Task.continueWith(Task.java:329)
at com.parse.Task$11.run(Task.java:485)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
编辑:与此同时,在我的代码的另一部分中,我也使用saveInBackground方法保存了一个ParseUser对象。似乎在执行ParseUser对象的saveInBackground时,而saveClimbing调用的saveInBackground试图执行时,会导致ANR的发生。ANR仅在某些情况下发生,而不是每次都发生。我做错了什么吗?
编辑:这似乎是一个错误(https://developers.facebook.com/bugs/591538734311827/)。我将尝试使用新的Android Parse SDK来查看死锁是否消失。
ClimbApplication.climbingDao.update(l_climbing);
导致了ANR吗? - Barend