简体   繁体   English

请在使用“第一个离线方法”时说明android体系结构。

[英]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): 目前我在做(根据上图):

  1. mLiveData getLiveData from Repository 来自存储库的mLiveData getLiveData
  2. callback refreshFromDataBase() 回调refreshFromDataBase()
  3. mLiveData.addSource from LiveData of DataBase - what causes that Android View is updated quickly 来自数据库的mLiveData.addSourceLiveData是什么原因导致Android View快速更新
  4. callback refreshFromNetwork() 回调refreshFromNetwork()
  5. Rest updates DatabaseTable 休息更新DatabaseTable
  6. Database insert causes that LiveData add pushes changes to the View 数据库插入导致LiveData添加将更改推送到视图

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 问题

  • Should I wipe all Table before inserting new Items? 在插入新项目之前,我应该擦拭所有桌子吗? Livedata listen to the changes of List, so whenever anything changes (some item updated, some inserted, some removed) view will be updated Livedata会监听List的更改,因此无论何时发生任何更改(某些项目已更新,某些已插入,某些已删除),视图都将更新
  • Under Circumstances where all my Streams are Single (reason: they dispose by itself when onComplete), how to chain them sequentially in oder to restrict one stream rule? 在我所有流都是单一流的情况下(原因:它们在onComplete时自行处置),如何顺序地链接它们以限制一个流规则?

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 一些开始的链接: thisthisthisthis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM