Giter Site home page Giter Site logo

Comments (1)

qingmei2 avatar qingmei2 commented on May 1, 2024

@wjploop

感谢你这么认真的回复。

关于ViewDataBing调试的。在布局文件中data节点中的元素,其创建一般有多个依赖,若其中的依赖有错,编译只告知ViewDataBinding创建失败,没法追溯到错误源,具体是哪个依赖错误导致。 于这点,有什么好的解决办法吗?

这个我一直都没觉得是很大的问题,因为DataBinding的编译错误会很详细的列在IDE的Log中,当然,随着项目体积的增大,会报很多错误,这个使用多了就知道如何找到真正的错误日志并修改了,就好像Dagger2一样,一开始不熟悉的开发者可能会看着一大堆编译期报错不知所措,用多了,发现错误的修改非常简单,因为IDE已经提示的很详细了。

我的想法是,能不能在viewDelegate中监听到onCreate()事件再做相应事件?或是说,我是哪里理解错了?

这一点你理解的并没有错误,事实上,Google官方对于ViewModel的使用规范的定义就是不要在ViewModel中持有View层的引用。

但是有一点需要确认的是,google的这个规范是基于LiveData+ViewModel的,而RxJava并不能像官方组件一样,默认绑定View层的声明周期——正如你所说,在View层进行观察ViewModel层的状态是一个标准的规范,我进行了适当地修改。

这么做原因是,ViewModel状态和UI的绑定,绝大部分我都交给了DataBinding,很多地方(实际也有,但是我更多用于ViewModel的复用)我不需要再让View层这样去处理:

userInfo.observe(lifecycleOwner, Observer { 
            //operate the UI
 })

这些逻辑我声明在xml中,剩下的代码交给ViewModel内部自己处理就行了——当然,为了保证ViewModel中Rx不会内存泄露,我让ViewModel层持有了LifecycleOwner,这也是我更偏向使用Rx而非Livedata的结果。

官方的代码是标准的使用规范,而我添加了一部分基于这些规范之上的个人思考和尝试——这些异同中,也许有些地方你认为很不错,有些地方你认为不是很好,我都认可,因为这都是对的,代码的本身就代表着不同的**。

这些代码我会在今后不断的实践中修改迭代,也欢迎你的提问以及更多交流 🤝

from 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.