0

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.

Shadow
  • 4,168
  • 5
  • 41
  • 72
  • Can you clarify a little your problem? From your post I've got that you use reflection to invoke one of several static methods. And you can set at these methods context and structured data needed for processing by Room, can't you? Does your singleton-approach give you what you want and you just doubt if it optimal? Or it doesn't work on some reason? If you can call repository and you have context it seems you have all you need to work with Room and you don't need to bother about lifecycle awareness, do you? – sergiy tikhonov Sep 28 '20 at 20:30
  • @sergiytikhonov you nailed it, at the moment it is doing everything that I want without any issues, but my concern is whether this is the proper way to do it - you are correct. I couldn't find anything in the docs that could shine a light at such scenario so I am looking for this while it is still in early integration stage given the API being so big it would be best to start things the right way rather than afterwards. – Shadow Sep 29 '20 at 01:58
  • Well, then I think your approach with singleton is what you want. In addition you can think about decoupling repository's instantiation logic from `Contacts`-like classes with some dependency injection/service locator pattern. But it's more about general architecture of your application and I guess that's not the core of your question. – sergiy tikhonov Sep 29 '20 at 09:51

1 Answers1

0

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.

Rabhi salim
  • 486
  • 5
  • 17
Shadow
  • 4,168
  • 5
  • 41
  • 72