Reputation: 2276
I am programming a feature inside my app that syncs all the users favorites to Google Drive. The whole point of this feature is that all the devices of that user are in-sync.
There are somethings that are good to know:
The problem with the first point above is the way of comparing cloud and local storage. When the user removes a favorite, and the connection is bad, the favorite isn't removed on Google Drive. When launching the app that same favorite gets added again. The app can't be launched while offline so a favorite can't be removed offline.
I am thinking of a way to make some kind of 'changelog' to see that a favorite is removed or added, that contains the time that it has been added/removed, and somekind of device id. (I am thinking out loud now) A problem of this is that the changelog file gets quite large when using the app for a while (every favorite addition and removal is logged)
The problem that I am facing is not the code, but the way the syncing needs to work and check for favorites. I've tried submitting the favorites themselves to Google Drive, but that takes up to much space (and can't be debugged easily).
Please think out loud (in the comments haha), I am not sure how I would tackle this challenge and I need to be pointed in the correct direction by someone.
Any solution is welcome.
Upvotes: 1
Views: 710
Reputation: 6791
allows your application to receive
ChangeEvents
even when it is not running. For example your application could show a notification when a file or folder it cares about has changed, even if that change occurs while your application not running.
The following example code shows the logic you might implement to add a change subscription to a file:
DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
mSelectedFileId);
file.addChangeSubscription(mGoogleApiClient);
Handle change subscriptions events
To handle change subscription events you must create a class that extends DriveEventService
and overrides the onChange
method. This is where application-specific processing of the ChangeEvent
would occur.
public class MyDriveEventService extends DriveEventService {
@Override
public void onChange(ChangeEvent event) {
Log.d(TAG, event.toString());
// Application-specific handling of event.
}
}
Add this to your AndroidManifest.xml file
<application ...>
...
<service android:name=".MyDriveEventService" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
</intent-filter>
</service>
</application>
Your application will receive a ChangeEvent whenever an file or folder gets updated by another application on the device or the Drive API downloads changes from the server.
You can also try using Push Notifications (Drive Rest API):
The Drive API provides push notifications that let you watch for changes to resources. You can use this feature to improve the performance of your application. It allows you to eliminate the extra network and compute costs involved with polling resources to determine if they have changed. Whenever a watched resource changes, the Drive API notifies your application.
You can read the blog for push notification for more information.
Hope this helps!
Upvotes: 1