使用android nytimes商店库获取数据时遇到问题

时间:2019-07-03 20:03:55

标签: android kotlin store android-room

我有以下代码,我正试图使用​​NyTimes / Store库从API端点获取数据以对其进行缓存

我正在使用DI来按如下方式加载我的依赖项

  val appModule = module(override = true) {
        single<MyAppDatabase> {
            Room.databaseBuilder(androidApplication(), MyAppDatabase::class.java, "MJiraniRoomDB")
                .fallbackToDestructiveMigration()
                .build()
        }
        //DATABASE ACCESS OBJECTS
        factory<BlogDao> { get<MyAppDatabase>().blogDao() }
        factory<BlogLocalService> { BlogLocalService(get()) }
        factory<BlogRemoteService> { BlogRemoteService(get()) }
        factory<BlogStore> { BlogStore(get(), get()) }

我在RoomStore中使用商店库的服务在这里

package com.example.myapplication.service.BlogService

import com.example.myapplication.model.Blog
import com.nytimes.android.external.store3.base.Fetcher
import com.nytimes.android.external.store3.base.impl.BarCode
import com.nytimes.android.external.store3.base.impl.MemoryPolicy
import com.nytimes.android.external.store3.base.impl.StalePolicy
import com.nytimes.android.external.store3.base.impl.room.StoreRoom
import com.nytimes.android.external.store3.base.room.RoomPersister
import io.reactivex.Observable
import java.util.concurrent.TimeUnit

class BlogStore(val blogRemoteService: BlogRemoteService, val blogLocalService: BlogLocalService) {

    var fetcher = Fetcher<List<Blog>, BarCode> {
        blogRemoteService.fetchBlogs()
    }

    val persister = object : RoomPersister<List<Blog>, List<Blog>, BarCode> {

        override fun read(barCode: BarCode): Observable<List<Blog>> {
            return blogLocalService.fetchAll().toObservable()
        }

        override fun write(barCode: BarCode, blogList: List<Blog>) {
            blogLocalService.addBlogs(blogList)
        }
    }

    var memoryPolicy: MemoryPolicy = MemoryPolicy
        .builder()
        .setExpireAfterWrite(5)
        .setExpireAfterTimeUnit(TimeUnit.SECONDS)
        .build()

    var store = StoreRoom.from(fetcher, persister, StalePolicy.REFRESH_ON_STALE, memoryPolicy)

    fun getBlogs(): Observable<List<Blog>> {
        store.clear()
        return store.fetch(BarCode.empty())
    }
}

我将数据按如下方式拉到Recyclerview中

class RemotePostsFragment : Fragment() {
    val scopeProvider by lazy { AndroidLifecycleScopeProvider.from(this) }

    val myBlogStore: BlogStore by inject()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        var mRecyclerview = view.findViewById<RecyclerView>(R.id.myRemoteViewRecycler)


        var theAdapter = MyAdapter()
       myBlogStore.getBlogs()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .autoDisposable(scopeProvider)
            .subscribe({ bloglist ->
                theAdapter.blogItems = bloglist
            }, {
                Log.e("RemotePostFragment", "failed got message ${it.message}")
            })


        mRecyclerview.adapter = theAdapter
        mRecyclerview.layoutManager = LinearLayoutManager(this.context)
    }

问题是我只从android room db加载数据,但从未从服务器获取数据。当我从提取程序中使用的服务器上进行原始查询时,我能够获得博客列表。但是,当我在提取程序中使用它时,我无法自动同步。

我的blogRemoteService单独测试时正确返回Single。我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

我在我的RoomPersister中的write方法中找到了解决问题的方法,我正在传递blogLocalService.addBlogs(blogList),该返回的Single<List<Blog>>我忘记订阅了。这就是我以前的

val persister = object : RoomPersister<List<Blog>, List<Blog>, BarCode> {

        override fun read(barCode: BarCode): Observable<List<Blog>> {
            return blogLocalService.fetchAll().toObservable()
        }

        override fun write(barCode: BarCode, blogList: List<Blog>) {
            blogLocalService.addBlogs(blogList)
        }
    }

我错误地认为Singe >>方法将以某种方式自动被订阅。这是我最后的有效代码

val persister = object : RoomPersister<List<Blog>, List<Blog>, BarCode> {

        override fun read(barCode: BarCode): Observable<List<Blog>> {
            return blogLocalService.fetchAll().toObservable()
        }

        override fun write(barCode: BarCode, blogList: List<Blog>) {
            blogLocalService.addBlogs(blogList).
            .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.io())
                    .subscribe({
                    Log.d("BlogLocalService","successful write to db")
                },{
                    Log.d("BlogLocalService","failed to write to db with error: ${it.message}")
                })
        }
    }