How to properly use Android Room in background tasks (unrelated to UI)

369 views Asked by At

At the moment Room is working well with a DB to UI integration:

Dao for DB operations

Repository for interacting with the Daos and caching data into memory

ViewModel to abstract the Repository and link to UI lifecycle

However, another scenario comes up which I am having a hard time understanding how to properly implement Room usage.

I have a network API that is purely static and constructed as a reflection of the servers' REST architecture.

There is a parser method that walks through the URL structure and translates it to the existing API via reflection and invokes any final method that he finds.

In this API each REST operation is represented by a method under the equivalent REST naming structure class, i.e.:

/contacts in REST equates to Class Contacts.java in API

POST, GET, DELETE in rest equates to methods in the respective class

example:

public class Contacts {
    public static void POST() {
        // operations are conducted here
    }
}

Here is my difficulty; how should I integrate ROOM inside that POST method correctly/properly?

At the moment I have a makeshift solution which is to instantiate the repository I need to insert data into and consume it, but this is a one-off situation everytime the method is invoked since there is absolutely no lifecycle here nor is there a way to have one granular enough to be worthwhile having in place (I don't know how long I will need a repository inside the API to justify having it cached for X amount of time).

Example of what I currently have working:

public class Contacts {
    public static void POST(Context context, List<Object> list) {
        new ContactRepository(context).addContacts(list);
    }
}

Alternatively using it as a singleton:

public class Contacts {
    public static void POST(Context context, List<Object> list) {
        ContactRepository.getInstance(context).addContacts(list);
    }
}

Everything works well with View related Room interaction given the lifecycle existence, but in this case I have no idea how to do this properly; these aren't just situations where a view might call a network request - then I'd just use networkboundrequest or similar - this can also be server sent data without the app ever requesting it, such as updates for app related data like a user starting a conversation with you - the app has no way of knowing that so it comes from the server first.

How can this be properly implemented? I have not found any guide for this scenario and I am afraid I might be doing this incorrectly.

EDIT: This project is not Kotlin as per the tags used and the examples provided, as such please provide any solutions that do not depend on migrating to Kotlin to use its coroutines or similar Kotlin features.

1

There are 1 answers

0
Shadow On BEST ANSWER

Seems like using a Singleton pattern, like I was already using, is the way to go. There appears to be no documentation made available for a simple scenario such as this one. So this is basically a guessing game. Whether it is a bad practice or has any memory leak risks I have no idea because, again, there is just no documentation for this.