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?