Ask one request to greenDAO to ignore the session cache

I am currently tracking individual changes to some objects in my application. This already works great by comparing the current object (the user modified copy) with the one in the database.

public void syncChanges(GsonTask current, GsonTask old) {
    // Load previous version of object if not provided.
    if (old == null) {
        // Here I get the old object from the database, while the user modified one is not touched.
        old = TasksService.getInstance().loadTask(current.getId());
    }

    // Bunch of logic to compare the current and old objects and track changes...

    // Another bunch of logic to sync the changes found to the server.

    // Save the current state of the object to the database (replacing the old one).
    TasksService.getInstance().saveTask(current);
}

      

But this approach presents performance issues as I need to copy the objects I get from greenDAO so that I can compare them later. So, in essence, I am not using the session cache to my advantage, since new objects (copies) are always created after requests.

public List<GsonTask> loadAllTasks() {
    // Load data from greenDAO and create the user modifiable copies.
    return gsonFromTasks(mExtTaskDao.listAllTasks());
}

private List<GsonTask> gsonFromTasks(List<Task> tasks) {
    List<GsonTask> gsonTasks = new ArrayList<GsonTask>();
    if (tasks != null) {
        // Create the copies.
        for (Task task : tasks) {
            gsonTasks.add(new GsonTask(...));
        }
    }
    return gsonTasks;
}

      

To eliminate this performance bottleneck, I now use the objects returned by greenDAO directly, without creating copies. This makes loading the list much faster, but now I can no longer track changes. When I try to compare the current object with the one in the database, my query returns the already changed object (as expected in the session cache).

public List<Task> loadAllTasks() {
    List<Task> tasks = mExtTaskDao.listAllTasks();

    // Return the greenDAO objects directly, without creating copies.
    return tasks != null ? tasks : new ArrayList<Task>();
}

public void syncChanges(GsonTask current, GsonTask old) {
    if (old == null) {
         // Here I can no longer load the previous state of this object (i.e. what actually persisted in the database).
         // old is now just a reference to current.
         old = TasksService.getInstance().loadTask(current.getId());
    }

    // Comparison between the user modified object and what in the database can't happen, as both objects are the same. 

    // I get sad. :/ Syncing changes is no longer possible.

    // Saving the current state of the object still works, as expected.
    TasksService.getInstance().saveTask(current);
}

      

I don't want to disable the session cache because the performance gain is significant. I would also hate to clear it with the help daoSession.clear()

, as the above code runs very often and the cache will have to be rebuilt all the time, making it impossible to improve performance.

So, is there a way to ignore the session cache for just one request? Or somehow ask for a new object instead of an existing reference?

public Task selectTask(Long id) {
    // Here I'd like to receive a new object, not the existing reference.
    return mDao.queryBuilder().where(TaskDao.Properties.Id.eq(id)).unique();
}

      

When I query an object from the database to compare against its custom modified state, I would like to be able to retrieve the new object exactly as it was stored in the database, rather than a link to what was loaded earlier. Is it possible somehow?

+3


source to share





All Articles