Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

几个优化的地方 #43

Open
Wheats opened this issue Nov 7, 2020 · 6 comments
Open

几个优化的地方 #43

Wheats opened this issue Nov 7, 2020 · 6 comments

Comments

@Wheats
Copy link

Wheats commented Nov 7, 2020

1.实现网络类
NetWorkViewModel 中可看出来 DemoApiService ,每请求一次需要create一次,这一块是可以不必要的
2.BaseViewModel 这个类会被业务多次集成,对应的Model也会重复写,引发网络处理多次重写代码
3.BaseViewModel里stateLiveData来对网络做一个标识,现在正在思考这个思路,单个请求可以这么玩,但是当多个请求的时候,对应结果集体输出,上层无法准确知道对应处理

目前优化了,
1.DemoApiService在AppApplication里init,静态单列,
2.BaseViewModel里增加公用ApiHelper 辅助类 集中处理结果
3.BaseViewModel 初始化ApiHelper对象 对应处理类持有BaseViewModel,即可使用stateLiveData

正在修改多网络请求结果问题

@wzqjava
Copy link
Owner

wzqjava commented Nov 9, 2020

多谢您的建议.
1-可以优化提交代码
2-之前封装有BaseModel层, 多人建议后去除了,大部分中小企业用不到.去除原因是,许多开发者对网络框架看不懂的话不敢用,(对于绝大部分页面一两个请求在viewmodel中操作即可,有自己需要的页面,可以封装model或repositorys,处理不同的数据来源).
3-多网络请求,根据 what值来判断不同接口, 请参考(TestNetFragment, TestNetViewModel--->getPersonalSummary()方法, 传入的what值,用来判断是哪个请求)
欢迎您优化并提交代码,加油!

@Wheats
Copy link
Author

Wheats commented Nov 9, 2020

1.现在目前正在测试优化后的部分,无问题可以调教代码
2.现在正在考虑是否去掉stateLiveData作为网络回调状态,通过Callback.Listener作为回调,但是有没有结合到集成BaseViewModel的设计存在意义,你提供的getPersonalSummary()未看到代码,也是一种可实现
3.代码接入第三天,思路正在完善整理

@wzqjava
Copy link
Owner

wzqjava commented Nov 9, 2020

getPersonalSummary()的代码,您先参考这个临时分支吧, https://github.com/wzqjava/MVVMSmart-kotlin, 底层有根据what值来判断的逻辑, 使用的是listener, 若不喜欢用rxJava的话,可以参考这个,使用listener来实现.
另外最近在思考出一版协程的,但暂时不打算加入此项目,因为很多开发者对协程不熟悉. 欢迎优您化.

@Wheats
Copy link
Author

Wheats commented Nov 9, 2020

暂时Callback.Listener 的回调已经使用上了,
如果在onNext回调里用postValueAndSuccess使用这个方法,需要回调里埋点action标签,上层解析action,对应做UI处理,尝试修改一下,

@Wheats
Copy link
Author

Wheats commented Nov 9, 2020

目前做了微调整,在StateLiveData里增加 action,
上层根据action监听,处理不同逻辑,目前改动最小,实现了,多请求公用数据分发
目前,把状态分为Loading Success Error
数据单独监听action的变化

mBaseViewModel.stateLiveData.postValueAndSuccess(action, response.getData());

@frankjiayounuli
Copy link

目前做了微调整,在StateLiveData里增加 action,
上层根据action监听,处理不同逻辑,目前改动最小,实现了,多请求公用数据分发
目前,把状态分为Loading Success Error
数据单独监听action的变化

mBaseViewModel.stateLiveData.postValueAndSuccess(action, response.getData());

请问有提交优化代码吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants