线程中的ParseUser和ParseObject会导致ANR

时间:2014-12-07 14:38:35

标签: java android multithreading parse-platform

我的Android应用程序存在问题。当应用程序尝试保存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正在执行时导致ANR,并且saveClimbing调用的saveInBackground尝试执行。 ANR有时只发生,而不是每次执行。我做错了吗?

编辑:这似乎是一个错误(https://developers.facebook.com/bugs/591538734311827/)。我将尝试使用新的Android Parse SDK来查看死锁是否消失。

1 个答案:

答案 0 :(得分:1)

解决:我使用的是Parse Android SDK v1.7.1,它有一个bug。此错误会导致死锁,并导致UI阻塞并生成ANR。新版本1.8.0修复了该错误。