1

I am using the awareness API on Android to receive a broadcast when the user enters a specific area. When I developed this functionality by the first time, I had about 500 locations and it worked fine. However, I have now more than 3000 locations, and when I unregister and update the fences, it takes too long (about 5 minutes) to update, or sometimes it gets stuck and do nothing.

I create the awareness fence like this

for (...) {
    ...
    AwarenessFence singleFence = LocationFence.entering(latitude, longitude, radius);

    individualFences.add(singleFence);
}

AwarenessFence allLocationsFence = AwarenessFence.or(individualFences);

where individualFences is a list containing more than 3000 fences. Then, I unregister and register again the fences by running

private void unregisterFence(final String fenceKey) {

    Awareness.FenceApi.updateFences(
            googleApiClient,
            new FenceUpdateRequest.Builder()
                    .removeFence(fenceKey)
                    .build()).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            registerFence(individualFences)
        }
    });
}

private void registerFence(AwarenessFence individualFences) {
    Awareness.FenceApi.updateFences(
            googleApiClient,
            new FenceUpdateRequest.Builder()
                    .addFence(LOCATION_FENCE_KEY, individualFences, fencePendingIntent)
                    .build()
    ).setResultCallback(new LocationFenceRegistrationResultCallback());
}

I build and connect to the google API client on an intent service. I build a different google API client on the main activity on my application.

I make the following test. I let the fence to register by the first time, then I try again after one minute, then again after one minute.

  • With 100 locations, the updates takes about 1 second every time
  • With 1000 locations or more, the first-time registration takes one second, the second one about 5 minutes and the third one takes much longer. But if I reboot the phone, the update takes again only one second.
  • With 1000 locations, the second registration takes only one second if I wait about 10 minutes.

How can I have always a short update time, when I attempt to update the fences repeatedly (with short interval)? Off course, I do not want the fences to be updated that often on release version, but for testing purposes it would be very useful.

And having such a large number of fences registered can have a serious impact on battery life or device performance?

Nicolás Arias
  • 1,053
  • 1
  • 12
  • 29

1 Answers1

0

It seems to me that you are simultaneously creating fences around locations geographically spread out over a large area (since you mention 3000 locations). It is conceivable that the time taken has some correlation with the number of fences added to the request. Perhaps you can divide up the locations in a hierarchical fashion and manage which fences are registered/ unregistered once you get fence callbacks.

For example, to register fences around various neighbourhoods in multiple cities, perhaps register the fences corresponding to only the current city along with another one that encompasses the entire city, and if device exits city, then unregister the ones in the old city and register the ones in the new city?

Hope this little toy example helps communicate my idea of managing which fences you request to register from your large list of fences.

MRC
  • 56
  • 4