如何在后台任务中正确使用Android Room(与UI无关)。

huangapple go评论57阅读模式
英文:

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

问题

目前,Room与数据库到UI的集成工作正常:

用于数据库操作的Dao
与Daos交互并将数据缓存到内存中的Repository
抽象Repository并与UI生命周期连接的ViewModel

然而,另一种情况出现了,我很难理解如何正确实现Room的使用。

我有一个纯静态的网络API,它是根据服务器的REST架构进行反射构建的。

有一个解析器方法,它遍历URL结构并通过反射将其翻译成现有API,并调用找到的任何最终方法。

在这个API中,每个REST操作都由等效的REST命名结构类下的方法表示,例如:

在REST中的/contacts等同于API中的Contacts.java类

在REST中的POST、GET、DELETE等同于各自类中的方法

示例:

public class Contacts {
    public static void POST() {
        // 在此执行操作
    }
}

以下是我的困难所在:如何在那个POST方法中正确/适当地集成ROOM?

目前,我有一个权宜之计的解决方案,即实例化需要插入数据的repository并使用它,但每次调用方法时都是一次性的,因为这里绝对没有生命周期,也没有足够精细的生命周期可以值得使用(我不知道我需要在API内部使用存储库的时间有多长,以证明将其缓存X时间是合理的)。

我目前实现的示例:

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

或者使用它作为单例:

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

在与View相关的Room交互方面,一切都运行正常,鉴于生命周期的存在,但在这种情况下,我不知道如何正确实现;这些不仅仅是视图可能调用网络请求的情况 - 那么我只会使用networkboundrequest或类似的方法 - 这也可能是服务器发送的数据,而应用程序从未请求过,例如像用户开始与您对话这样的应用程序相关数据的更新 - 应用程序无法知道这一点,因此它首先来自服务器。

如何正确实现这一点?我没有找到针对这种情况的任何指南,我担心我可能做错了。

编辑:根据所使用的标签和提供的示例,此项目不是Kotlin,因此请提供不依赖于迁移到Kotlin以使用其协程或类似的Kotlin功能的任何解决方案。

英文:

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&lt;Object&gt; list) {
        new ContactRepository(context).addContacts(list);
    }
}

Alternatively using it as a singleton:

public class Contacts {
    public static void POST(Context context, List&lt;Object&gt; 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

得分: 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.

huangapple
  • 本文由 发表于 2020年9月27日 04:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64082299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定