Search code examples
androidfilerealm

How to get last Modified RealmObject or fields in RealmObject with android


My application is running as follows:

  • Download the zip file from the url

  • Extract downloaded files, get csv files and folders audio

  • Save the csv file to the realm

  • delete zip files, fle extract, csv file, leaving only folder audio

  • show data from realm.

The problem here is that when I update the data from server, I want to update the data in the realm by comparing the last modified file from url with file in device but I deleted zip file, extract file, csv file, leaving only folder audio with mp3 file. When I get the last Modified mp3 file, it just fetches the date I last updated on the server, not the date I extracted it on the device. Should it always be smaller or equal to the update file on the server.

Code Sample

function extract file zip

private void extractZipe() {
  publishProgress(-1);
    try {
        ZipFile file = new ZipFile(zipFile);
        if (file.isEncrypted()) {
            file.setPassword("abcde12345-");
        }
        file.extractAll(zipFile.getParent());
        zipFile.delete();
        File csv = new File(context.getFilesDir(), "Data.csv");
        DataManager.getInstance().readTestQuestion(context, name, csv);
        csv.delete();
    } catch (ZipException e) {
        e.printStackTrace();
    }
}


//Realm data
public void readTestQuestion(Context context, String name, File file) {
    Realm realm = RealmManager.getDefault(context);
    Test test = Test.findByName(realm, name);
    String dateUpdate = new Date().toString();
    try {
        CSVReader reader = openCsvFromFile(file);
        List<Question> questions = readQuestion(realm, reader, test.getName());
        realm.beginTransaction();
        test.getQuestions().addAll(questions);
        realm.commitTransaction();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Check lastModified from url

// Get LastModified file from server, it's true
HttpURLConnection connection = ....
long dt=connection.getLastModified();
if (dt == 0)
    Log.d("carot", "download: No date information.");
else
    Log.d("carot", "download: "+ new Date(dt)+ "::"+ dt );

code check lastModified file in device

// Get LastModified file mp3 from device

File file = new File(holder.getContext().getFilesDir() + "/Data/Audio/", test.getQuestions().get(0).getAudio() + ".mp3");
long dt = file.lastModified();
if (dt == 0)
    Log.d("carot", "click: No date information.");
else
    Log.d("carot", "click: " + new Date(dt) + "::" + dt);

Realm Object

//Realm Object Test with fields lastUpdated, created ..Always null because I did not process them before

public class Test extends RealmObject{
@PrimaryKey
    @Required
    private String id;
    private String name;
    private int duration;
    private int number_of_questions;
    private int display_order;
    private int progress;
    private String path;
    private boolean is_deleted;
    private String lastUpdated;
    private String created;
    private RealmList<Question> questions;
}

\\Question RealmObject 
public class Question extends RealmObject{
@PrimaryKey
    @Required
    private String id;
    @Index
    private String audio;
    @Index
    private String photo;
    private String text;
    @Index
    private String transcript;
    private String typeName;
    private int display_order;
}

(Realm version(s): 1.2.0)


Solution

  • 1.) add a no-arg constructor that sets lastModified date to new Date().

    2.) increase your schema version to +1 of whatever it is now, and define a migration that would set the value of lastModified to new Date() where lastModified is currently null.