0
votes

Good afternoon to all.
I have quite simple notes application and I want to make simple thing - on user's demand app should synchronize its "notes_db" file with version in cloud
(at first stage I want to synchronize the whole "notes_db" file, for simplification).

As I'm not skilled enough yet to write my own web-server with its API, it was decided to use DropBox Sync API for my purposes.
At this moment I've successfully implemented user authorization in my app.
And I supposed, that I will indicate "notes_db" file and say "Sync this file!" to Sync API - and this file will be synced to cloud.
But it isn't so easy for me now :-(.

My first question is - do I understand correct, that I'm not able to sync specific file that I want to sync (in my case, this is file located at "/databases/notes_db") via DropBox Sync API?
And the second question - what should be mechanism of synchronization in that case?

My guess is that I should do next
(I guess that I will have 3 versions of "notes_db" file - local (where my app will make changes), local cache and cloud version):
1) create empty file "notes_db" in DropBox filesystem (if I understand right, it will create both local cache and cloud versions) or try to open it, if it exists;
2) transfer contents of local version to just created local cache version;
3) call some methods (which?) to syncronize changes from local cache to cloud version.

Then, on next synchronization call:
1) compare files - cloud, local cache and local versions;
2) if local version is the newest one - transfer its contents to local cache version and start synchro;
3) if cloud version is the newest one - get it in local cache and transfer its contents to local version of file.

In addition - after creating DropBox filesystem, which methods should I call? hasSynced(), listFolder(), awaitFirstSync(), syncNowAndWait(), getSyncStatus()?
Saying honestly, I'm totally confused with it now.

Or may be there is a simplier way to solve my issue - no matter, via DropBox Sync API or in some other way?

If someone can provide useful tutorial or working example with synchronization of one file - I would be very grateful for that.

And thanks for any help!
This question is really important for me now.

1

1 Answers

0
votes

Ok, friends.
Still don't know, is it a best way to solve my issue, but it's working and solved my problem.
Based on mechanism, described above:

First step.
Get dbxFileSystem (providing we already have linkedAccount) and decide, which operation is needed to perform:

try {
   dbxFileSystem = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
   if (dbxFileSystem.exists(dbxPath)) {
      dbxFile = dbxFileSystem.open(dbxPath);
      if (_myNotesQuantityCount == 0) {
         try {
            writeFromCloud(dbxFile.getReadStream());
         } catch (IOException e) {
              Toast.makeText(error text).show();
         }
      }
      else {
         writeToCloud();
      }
   }
   else () {
      dbxFile = dbxFileSystem.create(dbxPath);
      writeToCloud();
   }
}

Second step.
Perform needed operation:
write local file to cloud version

public void writeToCloud() {
        try {
            dbxFile.writeFromExistingFile(_localDBLocation, false);
        } catch (IOException e) {
            Toast.makeText(error text).show();
        } finally {
            dbxFile.close();
        }
    }

or write cloud version to local file

public void writeFromCloud(FileInputStream inputStream) {
        try {
            outputStream = new FileOutputStream(_localDBLocation);
            byteBuffer = new byte[1024];
            int length;
            try {
                while ((length = inputStream.read(byteBuffer)) > 0) {
                    outputStream.write(byteBuffer,0,length);
                }
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                Toast.makeText(this, error text).show();
            } finally {
                dbxFile.close();
            }
        } catch (FileNotFoundException e) {
            Toast.makeText(error text).show();
        } finally {
            dbxFile.close();
        }
    }

The one issue I've faced was that synchronization to cloud is not immediate.
I suppose correct way was to set listener for file changes, but I just paused my thread a little and called synchronization again (there was a question on SOF about calling twice).

Now it's working! Anyway, if someone has something to improve or better solution - I'd be glad to hear it.