Search code examples
androidandroid-recyclerviewrx-java2android-mvvm

Infinite updating of the RecyclerView list


With the help of dagger and rxJava I update the list in a RecyclerView. Everything works well, the list is displayed. But the problem is that in the logs I see how this list is updated every second. What could be the problem? In a similar project but in Java everything works correctly, the list is updated once at startup. My Network Module:

@Module(includes = [ViewModelModule::class])
class NetworkModule {

    companion object {
        const val KEY = "key"
        const val BASE_URL = "base_url"
    }

    @Provides
    @Singleton
    fun provideOkHttp(): OkHttpClient {
        val httpClient = OkHttpClient.Builder()
        httpClient.addInterceptor(object : Interceptor {
            @Throws(IOException::class)
            override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
                val original = chain.request()
                val originalHttpUrl = original.url
                val url = originalHttpUrl.newBuilder()
                    //.addQueryParameter("apikey", KEY)
                    .build()
                val requestBuilder = original.newBuilder()
                    .url(url)
                    .header("apikey", KEY)
                val request = requestBuilder.build()
                return chain.proceed(request)
            }
        })
        // logging interceptor
        val logging = HttpLoggingInterceptor()
        logging.level = HttpLoggingInterceptor.Level.BODY
        httpClient.addInterceptor(logging)
        return httpClient.build()
    }

    @Provides
    @Singleton
    fun provideRetrofit(): Retrofit {

        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(provideOkHttp())
            .build()
    }

    @Provides
    @Singleton
    fun provideContactsService(retrofit: Retrofit) : ContactsService{

        return retrofit.create(ContactsService::class.java)
    }
}

My ViewModel:

class ContactsViewModel @Inject constructor(private val contactsRepository: ContactsRepository) :
    ViewModel() {

    var mutableLiveData = MutableLiveData<List<ContactsModel>>()
    private val disposable = CompositeDisposable()

    fun getContactMutableLiveData(): MutableLiveData<List<ContactsModel>> {
        loadData()
        return mutableLiveData
    }

    fun loadData() {

       disposable.add(contactsRepository.modelSingle()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object : DisposableSingleObserver<List<ContactsModel>>() {
                override fun onSuccess(t: List<ContactsModel>) {
                getContactMutableLiveData().value = t
                }

                override fun onError(e: Throwable) {
                }
            })
        )
    }
}

And my Activity:

contactsViewModel.getContactMutableLiveData().observe(this@ContactListActivity, Observer {

            mAdapter = ContactsAdapter(this@ContactListActivity, it as ArrayList<ContactsModel>)
            recycler_contacts.layoutManager =
                LinearLayoutManager(applicationContext, OrientationHelper.VERTICAL, false)
            recycler_contacts.adapter = mAdapter
            recycler_contacts.setHasFixedSize(true)

            mAdapter.sortByName()

        })

Solution

  • It was a logical error. You need to rewrite the loadData function as shown below

    class ContactsViewModel @Inject constructor(private val contactsRepository: ContactsRepository) :
        ViewModel() {
    
        var mutableLiveData = MutableLiveData<List<ContactsModel>>()
        private val disposable = CompositeDisposable()
    
        fun getContactMutableLiveData(): MutableLiveData<List<ContactsModel>> {
    
            disposable.add(contactsRepository.modelSingle()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(object : DisposableSingleObserver<List<ContactsModel>>() {
                    override fun onSuccess(t: List<ContactsModel>) {
                        mutableLiveData.value = t
                    }
                    override fun onError(e: Throwable) {
                    }
                }))
            return mutableLiveData
        }
    
    }