Comments (6)
@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.
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.
@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.
@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.
@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.
just corrected something 👍🏿
from android-mvvm-architecture.
Related Issues (20)
- Service calling issue HOT 1
- Will there be an example with live data?
- performDataBinding() take too much time HOT 8
- blogViewModel and openSourceViewModel VS ViewModelProviderFactory HOT 2
- recyclerview adapter received null list from livedata in my own implementation
- gradle 5.1.1 is not supported HOT 1
- How to achieve componentization
- Data Binding Error
- T is a type-variable HOT 2
- Caution : Repository is Singleton HOT 1
- cannot find symbol class UI
- Using DaggerApplication, daggerAppCompathActivity, DaggerFragment HOT 1
- unit test
- DaggerAppComponent Not able to build HOT 4
- Cast Problem.
- Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with id 'findbugs' not found. HOT 2
- How to handel error codes in this patterns for api calls
- Same project with kotlin
- How to get data from dialog
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-mvvm-architecture.