0
\$\begingroup\$

I have been developing an Android application, the application has a Room database which will have changes written to it. When the user leaves the application, then the database file is copied to the Firebase Storage. Now once the user opens the application again, then the process goes.

  1. Download the master database file from the Firebase Storage
    private void downloadMasterDatabase(){
        storageRef = FirebaseStorage.getInstance().getReference();
        StorageReference pathReference = storageRef.child("master database" + "/master_table");
        String masterDbPath = "/data/data/"+ getPackageName() + "/databases/master_table";
        File masterDbFile = new File(masterDbPath);
        pathReference.getFile(masterDbFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                Log.d(TAG, "onSuccess: " + 1);
                loadDbFromFirebase();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Can't update database, maybe there is no internet connection", Toast.LENGTH_LONG).show();
            loadDbFromFirebase();
            }
        });
    }
  1. Download the user's database file from the Firebase Storage
    private void loadDbFromFirebase(){
        String currentDBPath = "/data/data/"+ getPackageName() + "/databases/locations_table";
        File currentDbFile = new File(currentDBPath);
        if (userId != null && !currentDbFile.exists()){
            StorageReference pathReference = storageRef.child("users").child(userId).child("user database").child("locations_table");
            pathReference.getFile(currentDbFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Log.d(TAG, "onSuccess: " + 2);
                    updateDatabaseData();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    updateDatabaseData();
                }
            });
        }else{
            updateDatabaseData();
        }
    }
  1. Open the user's database and save the amount of items in a global integer. Then it opens the master database file to check for changes.
  private void updateDatabaseData(){

        markerViewModel.getAllMarkers().observe(this, new Observer<List<MarkerObject>>() {
            @Override
            public void onChanged(List<MarkerObject> markerObjects) {
                listSize = markerObjects.size();
            }
        });
        masterViewModel.getAllMarkers().observe(this, new Observer<List<MasterMarkerObject>>() {
            @Override
            public void onChanged(List<MasterMarkerObject> masterMarkerObjects) {
                insertMarkers(masterMarkerObjects);
            }
        });
    }
  1. The previous method got the listSize of the user's database file to now check if the master database file has additional items that the user's list doesn't have. If the master database has more items than the user's, then all the items that have an id that is bigger than the number of entries in the user's database, will be copied to the user's database as new entries. Finally it navigates to the MainActivity from which the application does all the other necessary actions.
 private void insertMarkers(List<MasterMarkerObject> masterMarkerObjects){
        for (MasterMarkerObject masterMarkerObject : masterMarkerObjects){
            if (masterMarkerObject.getId() > listSize ){
                MarkerObject markerObject = new MarkerObject(
                        masterMarkerObject.getId(),
                        masterMarkerObject.getTitle(),
                        masterMarkerObject.getDescription(),
                        masterMarkerObject.getLatitude(),
                        masterMarkerObject.getLongitude(),
                        masterMarkerObject.getActive(),
                        masterMarkerObject.getCounty(),
                        masterMarkerObject.getCategory(),
                        masterMarkerObject.getImage(),
                        masterMarkerObject.getExperience(),
                        masterMarkerObject.getDifficulty());
                markerViewModel.insert(markerObject);
            }
        }Intent intentToMain = new Intent(StartActivity.this, MainActivity.class);
        startActivity(intentToMain);
        finish();
    }

The process works but it seems a bit resource intensive, currently I have only used a database file which has around 30 entries but it will have thousands once it is finished. So long story short, does anyone have a better solution to updating a SQL database file within an Android application?

The application also has two more Room databases which will also have to be updated and that is why I am checking for alternative solutions, because the process of downloading 2 files, then opening these two files with two ViewModels to check for and then implementing updates seems to me a bit clunky, especially where I would repeat the same process 3 times every time the application starts up.

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.