-1

I have Content provider and sync adapter in my project. In my project first i'm going to fetch data from server and then i'm storing it in my database sqlite.

Now i'm coming to my issue. Actually i want to change the server data according to the data modified in the sqlite.

For example: if i delete data in my database i also want to delete data in my server database. For this purpose my sync adapter should know what operation is performed like delete or insert.

Another way of doing this is comparing my whole local sqlite database with the data in the server when the sync adapter is invoked.

I don't want that approach. Is there any way to know who is invoking sync adapter or what operation is performed in content provider from sync adapter?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Dinesh S
  • 139
  • 2
  • 13

1 Answers1

0

The term "who is invoking sync adapter" seems not valuable, as long as that would give you no useful information. Instead, you most probably would like to pass some params at the time when someone is asking for a sync (via requestSync()).

ContentResolver.requestSync(Account, String, Bundle) accepts an extra Bundle that will be passed to your SyncAdapter's onPerformSync():



    // a code that initiates sync 
    ContentResolver.requestSync(ACCOUNT, AUTHORITY, someBundle);

    ...

    // your implmementation of SyncAdapter
    @Override
    public void onPerformSync(
            Account account,
            Bundle extras, // acquire information from this bundle
            String authority,
            ContentProviderClient provider,
            SyncResult syncResult) {
           /*
            * Put the data transfer code here.
            */
            ...
    } 

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • Is there any other way. – Dinesh S Aug 08 '18 at 12:12
  • This is the legitimate/suggested way. Of course you can do it the bad way, e.g. save some data in shared preferences and read that data, but... just don't do that. What's the issue with this approach? – azizbekian Aug 08 '18 at 12:16
  • I want to know what operation is performed in content provider from sync adapter. – Dinesh S Aug 08 '18 at 13:50
  • You can pass the query via bundle to the `SyncAdapter`, thus you will know what exact operation was performed by `ContentProvider`. Basically, you need to pass some kind of key from one execution point to the other. – azizbekian Aug 08 '18 at 14:24
  • Thanks. Now i assume that there is no way to pass data to sync adapter other than bundle. – Dinesh S Aug 08 '18 at 14:40
  • Nope, there is no other legitimate way. – azizbekian Aug 08 '18 at 15:24