Google Fitness: Dealing with TransactionTooLargeException without feedback

冷暖自知 提交于 2021-01-28 01:08:04

问题


I'm building an application integrating with Google Fit via the local (not REST) Google Fit API. I'm using Flutter 1.2.1, Kotlin 1.3.21, com.google.android.gms:play-services-fitness:16.0.1 and running the app on Android 8.1.0 with Google Play Services 16.0.89.

Some HistoryClient.readData() requests disappear without any feedback (no calls to onSuccessListener, onCancelledListener, onFailureListener, onCompleteListener callbacks), and at these times I can see in the logs:

   W/Fitness: Error delivering batch 0. Attempt #2
    android.os.TransactionTooLargeException: data parcel size 572360 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:777)
        at crb.c(:com.google.android.gms@16089017@16.0.89 (040306-239467275):1)
        at yek.a(Unknown Source:8)
        at yta.a(:com.google.android.gms@16089017@16.0.89 (040306-239467275):52)
        at yta.a(:com.google.android.gms@16089017@16.0.89 (040306-239467275):31)
        at ytc.run(:com.google.android.gms@16089017@16.0.89 (040306-239467275):44)
        at rrt.b(:com.google.android.gms@16089017@16.0.89 (040306-239467275):32)
        at rrt.run(:com.google.android.gms@16089017@16.0.89 (040306-239467275):21)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at rxx.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:764)

My understanding is that either the local Fitness service (part of Google Play Services) fails to send a notification about failure, or that the client library to Google Fit that I'm using fails to pass it along. Is this correct?

How are others using this API to workaround this problem? What's a good timespan to ask for to be reasonably certain that it will complete successfully on all phones?

Note: I tried posting this as a bug on https://issuetracker.google.com/issues/130402455, but I didn't get a response and I'm not even sure if it was the right place for that.


回答1:


TransactionTooLargeException means the data exchange in between services/applications of any kind exceeded the Android's allowed data upper limit. Usually, the size limit is around 200KB, but it could vary (and I'm not 100% sure).

The solution would be to split up the data you're requesting, looking through the Google Fit API, I think you can try get less data at a time by setting a shorter time range in the DataRequest. In your case, the error says you're requesting around 500KB of data, which means you could try to shorten your request's time span to around half.

But even then the actual data size could vary by a lot depending on the user's activities, and it's not ideal to request too little at a time for many times, so I think you could try setting up a timer after your request, and if over some time, the data is not returned, reduce the request's time span and try again (since capturing the error is impossible).

Another approach to catch the error would be to read the logs in a separate thread, and if it catches the TransactionTooLargeException, it will callback and notify you to reduce the request's time span. You can capture the logs like this:

try {
  Process process = Runtime.getRuntime().exec("logcat *:S Fitness:W");//only show Google Fitness related logs
  BufferedReader bufferedReader = new BufferedReader(
  new InputStreamReader(process.getInputStream()));
  String line = "";
  while ((line = bufferedReader.readLine()) != null) {
     if(line.contains("Error delivering batch")){
        //do your callback here
     }
  }
  } 
catch (IOException e) {}

And for no exception being thrown, I'd say that's a bug on Google's end since they didn't consider this situation and catch that exception, and it would be a valid bug report.



来源:https://stackoverflow.com/questions/55758682/google-fitness-dealing-with-transactiontoolargeexception-without-feedback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!