Coder Social home page Coder Social logo

Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context. about android-mvvm-architecture HOT 6 OPEN

janishar avatar janishar commented on August 25, 2024
Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

from android-mvvm-architecture.

Comments (6)

ArthurSav avatar ArthurSav commented on August 25, 2024 4

@clj0020 It holds a reference to the object that implements the interface, in this case the activity. There are better ways to notify the UI, try using Live Data.

from android-mvvm-architecture.

ameybhandarkar avatar ameybhandarkar commented on August 25, 2024

I am trying to implement MVVM in my current project. I was searching for some references. I just to clear a few doubts. So you can correct me if I am wrong.

mLoginViewModel.setNavigator(this);

This line of code is actually passing on the reference of the current activity to that viewmodel. But as per documentation, Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.
So is there any alternative to this?

I am also searching for an approach of handling errors thrown by the API. I'd be glad if you can guide me through. Thanks

from android-mvvm-architecture.

clj0020 avatar clj0020 commented on August 25, 2024

@ameybhandarkar the navigator is an interface between the activity and the viewmodel. That is not holding a reference to a context or really anything. There is a special kind of viewmodel called the AndroidViewModel that does hold reference to a context.

from android-mvvm-architecture.

nguyenvanminhfptpoly avatar nguyenvanminhfptpoly commented on August 25, 2024

@ameybhandarkar have idea for replace Weak references

in ViewModel i create
///////////////////////////////
data class Input( val name: Observable<String>, val image: Observable<ByteArray>, val triggerAdd: Observable<Unit> ) data class Output( val isAdd: Observable<Boolean> )

fun transform(input: Input): Output{
    val nameImage = BehaviorSubject.create<String>()
    val imageLibrary = BehaviorSubject.create<ByteArray>()
    val isAdd = BehaviorSubject.create<Boolean>()


    with(input){
        name.subscribe(nameImage)
        image.subscribe(imageLibrary)
        triggerAdd.subscribe {
            val name = nameImage.value ?:""
            val image = imageLibrary.value ?: byteArrayOf()
            dataManager.insert(Library(null,name,image))
                .subscribeOn(schedulerProvider.io)
                .observeOn(schedulerProvider.ui)
                .subscribe ({
                    isAdd.onNext(it)
                },{
                    Log.d("addImage",it.message)
                })
        }

    }.addTo(compositeDisposable)
    return Output(isAdd)
}

//////////////////////////////
in Activity/Fragment i create
///////////////
private fun bindViewModel(){ val output = viewModel.transform( RoomImageViewModel.Input( edImage.textChanges().map { it.toString() }, image, btnInsert.clicks().throttleFirst(300, TimeUnit.MILLISECONDS) ) ) with(output){ isAdd.subscribe { Log.d("AddImage",it.toString()) } isDelete.subscribe { Log.d("Delete",it.toString()) } } }
//////////////

from android-mvvm-architecture.

androminor avatar androminor commented on August 25, 2024

@ameybhandarkar the navigator is an interface between the activity and the viewmodel. That is not holding a reference to a context or really anything. There is a special kind of viewmodel called the AndroidViewModel that does hold reference to an applictation context.

from android-mvvm-architecture.

androminor avatar androminor commented on August 25, 2024

just corrected something 👍🏿

from android-mvvm-architecture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.