[英]Please explain android architecture when using “First offline aproach”.
my app architecture, quite common: 我的应用程序架构很常见:
Please explain me if I have list of Entities, for example 例如,如果我有实体列表,请向我解释
@Entity(tableName = TABLE_NAME)
class Item constructor(
@PrimaryKey(autoGenerate = false)
var id: Long = 0L,
@TypeConverters(ListToStringConverter::class)
var eventDescription: List<String> = emptyList(),
@TypeConverters(DateConverter::class)
var date: Date = Date(),
var rate: Int ?= null)
Picture explanation: 图片说明:
Currently I do (according to picture above): 目前我在做(根据上图):
mLiveData
getLiveData from Repository 来自存储库的mLiveData
getLiveData refreshFromDataBase()
回调refreshFromDataBase()
mLiveData.addSource
from LiveData
of DataBase - what causes that Android View is updated quickly 来自数据库的mLiveData.addSource
的LiveData
是什么原因导致Android View快速更新 refreshFromNetwork()
回调refreshFromNetwork()
Formulation of the problem 问题的提法
What is the best practice for 5 step - a moment when new data comes and I have to replace old data with the freshest? 5步的最佳做法是什么-当有新数据出现时,我必须用最新鲜的数据替换旧数据?
Currently I'm using RxJava, Room, Kotlin and I'm using in step 3 nested Rx.Single which is very ugly spagetti code. 目前,我正在使用RxJava,Room,Kotlin,并且在第3步中使用了嵌套的Rx.Single,这是非常难看的spagetti代码。
like 喜欢
disposable = usecase.getDataFromNetwork()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onSuccess = {
val itemsList = it
dataBase.deleteAllItems()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy (onComplete = { dataBase.insertNewItems(itemsList)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onComplete = {
// Process complete
}, onError = {
...
})
}
}, onError = {
...
})
}, onError = {
...
})
Ugly spagetti code. 丑陋的意大利面代码。
QUESTIONS 问题
This depends if your fetched data will be similar or not. 这取决于您获取的数据是否相似。 If you fresh data is normally completly different I would delete all elements and then insert new elements with only one method like following: 如果您的新数据通常完全不同,那么我将删除所有元素,然后仅使用一种方法插入新元素,如下所示:
@Insert
public void insertItems(Item... items);
If your new data is similar and you don't care about their order I would compare all new items to the old ones to know for each item if you have to update, insert or delete item. 如果您的新数据相似,并且您不在乎它们的顺序,那么我将比较所有新项目和旧项目,以了解每个项目是否需要更新,插入或删除。 At the end you call deleteItems(itemsToDelete), updateItems(itemsToUpdate) and insertItems(itemsToInsert). 最后,您调用deleteItems(itemsToDelete),updateItems(itemsToUpdate)和insertItems(itemsToInsert)。 This three methods are similar as the one written above but with @Delete, @Update and @Insert. 这三种方法与上面编写的方法类似,但是使用@ Delete,@ Update和@Insert。
If you care about the order I recommend you take a look at Android Paging Library and create a custom DataSource where you can tell to your live data when to update its values. 如果您关心订单,我建议您看一下Android Paging Library并创建一个自定义DataSource,您可以在其中告诉实时数据何时更新其值。 Some links to start: this , this , this and this 一些开始的链接: this , this , this和this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.