为什么不用rxjava?
rxjava等系列产品.思想是很好的,但是被大多数人用成了一坨屎!
就拿rx最经典的那个例子来说:
假设有这样一个需求:界面上有一个自定义的视图 imageCollectorView ,它的作用是显示多张图片,并能使用 addImage(Bitmap) 方法来任意增加显示的图片。现在需要程序将一个给出的目录数组 File[] folders 中每个目录下的 png 图片都加载出来并显示在imageCollectorView 中。需要注意的是,由于读取图片的这一过程较为耗时,需要放在后台执行,而图片的显示则必须在 UI 线程执行。常用的实现方式有多种,我这里贴出其中一种:
new Thread() {
@Override
public void run() {
super.run();
for (File folder : folders) {
File[] files = folder.listFiles();
for (File file : files) {
if (file.getName().endsWith(".png")) {
final Bitmap bitmap = getBitmapFromFile(file);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
imageCollectorView.addImage(bitmap);
}
});
}
}
}
}
}.start();
猛一看有点繁琐,应该可以优化下,别急先看看rx怎么实现.
rxjava :
Observable.from(folders)
.flatMap(new Func1<File, Observable<File>>() {
@Override
public Observable<File> call(File file) {
return Observable.from(file.listFiles());
}
})
.filter(new Func1<File, Boolean>() {
@Override
public Boolean call(File file) {
return file.getName().endsWith(".png");
}
})
.map(new Func1<File, Bitmap>() {
@Override
public Bitmap call(File file) {
return getBitmapFromFile(file);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Bitmap>() {
@Override
public void call(Bitmap bitmap) {
imageCollectorView.addImage(bitmap);
}
});
猛一看mdzz(玛德制杖),仔细一看mdzzz(玛德真制杖)----> 代码写成这样不脑残么?知不知道什么叫做解耦合啊?
我的实现:
//----------开始加载所有图片
public void addAllImg(){
x.task().run(new Runnable() {//我用xutils3异步任务
@Override
public void run() {
for (File folder : folders) { //遍历文件夹
File[] files = folder.listFiles();
for (File file : files) { //遍历文件
addAllImg_1(file); //调用后续方法处理
}
}
}
});
} private void addAllImg_1(File file){ //处理,更新ui等
if (file.getName().endsWith(".png")) {
final Bitmap bitmap = getBitmapFromFile(file);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
imageCollectorView.addImage(bitmap);
}
});
}
}
//----------加载所有图片完毕
rx系列缺点太多了,任意一条都够理由不用他了 因为有更好的实现呀
1.过度使用设计模式,逻辑是只有一条但是代码被你们写成了屎呀 还链式也是醉人
什么叫链式,看我的实现... 支持继续扩展 2 3 4 5 6 ... n
private void addAllImg_2()
private void addAllImg_3()
...
2.好的思想不懂合理利用,看看Android-LessCallBack是怎么用的
//按id顺序执行的
public class Test2 {
@TaskFlow(thread = true, id = 0)
public void doTask1() {
System.out.println(Thread.currentThread().getName()+":"+"任务1");
waitTime();
}
@TaskFlow(thread = false, id = 1)
public void doTask1Finish() {
System.out.println(Thread.currentThread().getName()+":"+"任务1执行结束");
}
@TaskFlow(thread = true, id = 2)
public void doTask2() {
System.out.println(Thread.currentThread().getName()+":"+"任务2");
waitTime();
}
@TaskFlow(thread = false, id = 3)
public void doTask2Finish() {
System.out.println(Thread.currentThread().getName()+":"+"任务2执行结束");
}
public static void main(String[] args) {
TaskFlowUtils.inject(new Test2()).run();;
}
public static void waitTime(){
int x = new Random().nextInt(1000) + 500;
try {
System.out.println("等待"+x+"毫秒");
Thread.sleep(x);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
线程:任务1
等待515毫秒
ui线程:任务1执行结束
线程:任务2
等待655毫秒
ui线程:任务2执行结束
在我看来其与rx类似,都用到next这一思想,不同的是:
lesscallback 实现复杂,调用简单
rx实现复杂,调用也特么复杂
3.耦合度太大导致调试困难的问题...满眼的next subscribe observe
有写问题写时候,你可能没发现 维护时候你就懂了
4.学习难度大,想要吃个牛肉串你让我先学宰牛?
5.少用interface 如果可以,尽量不要用 (interface是一种挖洞行为,除非不可避免否则不要挖洞)
对于你们那些 网络请求还先写个interface的我表示无法理解 每个页面都有自己的功能,在各自的页面(函数里)实现自己的功能 更是一种分布式的思想
随便搜一下都是rx怎么怎么好,支持什么什么 可以扩展什么什么 巴拉巴拉... 真正有自己的理解的有几个?
一群小菜比
大道至简,如果理解深刻,用起来会非常简单,搞出来那么复杂的东西 只说明你们理解不太深刻.
为什么不用rxjava?的更多相关文章
- Retrofit结合RxJava使用指南
Retrofit结合RxJava使用指南 Retrofit是一个当前很流行的网络请求库, 官网的介绍是: "Type-safe HTTP client for Android and Jav ...
- 78. Android之 RxJava 详解
转载:http://gank.io/post/560e15be2dca930e00da1083 前言 我从去年开始使用 RxJava ,到现在一年多了.今年加入了 Flipboard 后,看到 Fli ...
- RxJava学习入门
RxJava是什么 一个词:异步. RxJava 在 GitHub 主页上的自我介绍是 "a library for composing asynchronous and event-bas ...
- 给 Android 开发者的 RxJava 详解
我从去年开始使用 RxJava ,到现在一年多了.今年加入了 Flipboard 后,看到 Flipboard 的 Android 项目也在使用 RxJava ,并且使用的场景越来越多 .而最近这几个 ...
- 一个在 Java VM 上使用可观测的序列来组成异步的、基于事件的程序的库 RxJava,相当好
https://github.com/ReactiveX/RxJava https://github.com/ReactiveX/RxAndroid RX (Reactive Extensions,响 ...
- RxJava 与 Retrofit 结合的最佳实践
转自:http://gank.io/post/56e80c2c677659311bed9841?from=timeline&isappinstalled=0&nsukey=g1D1Y6 ...
- android ------- 开发者的 RxJava 详解
在正文开始之前的最后,放上 GitHub 链接和引入依赖的 gradle 代码: Github: https://github.com/ReactiveX/RxJava https://github. ...
- 函数响应式编程RxJava
RxJava 到底是什么 一个词:异步. RxJava 在 GitHub 主页上的自我介绍是 "a library for composing asynchronous and event- ...
- 转:给 Android 开发者的 RxJava 详解
转自: http://gank.io/post/560e15be2dca930e00da1083 评注:多图解析,但是我还是未看懂. 前言 我从去年开始使用 RxJava ,到现在一年多了.今年加入 ...
随机推荐
- shell脚本实现算术运算且输入的不能为非数字的值
#!/bin/bash c= ] do echo "请输入第一个数" read a echo "请输入第二个数" read b ]* ]] && ...
- Windows7 忘记密码的解决办法
由于无法使用管理员帐号进入Windows 7,辅助工具比较大,已经回不到xp时代的pe一键删除密码了,不过用Windows 7的system账户运行cmd命令可以强制修改账户密码!就拿xp+Windo ...
- 一个C++版的嵌入式操作系统
原创文章,转载请注明出处! 现世面上流传着很多嵌入式操作系统,都已经非常优秀,但本人(Sam的博客-博客园)还是自己编写了一个RTOS,不敢说优秀,但绝对是使用起来最简单的.先看一个工程截图与一段m ...
- 谈c++ pb_ds库(二) 红黑树大法好
厉害了,没想到翻翻pb_ds库看到这么多好东西,封装好的.现成的splay.红黑树.avl... 即使不能在考场上使用也可以用来对拍哦 声明/头文件 #include <ext/pb_ds/tr ...
- 前端之web上传文件的方式
前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...
- grub2配置显示系统选择菜单(ubuntu 14.04)
背景: 有一次遇到过安装的新的内核后,ubuntu并没有显示选择内核的grub菜单,按shift键才会弹出,有时候shift按得不准就又进入了不是想进入的那个系统. 配置方法: sudo vim /e ...
- AutoMapper的使用
1.AutoMapper简单介绍 官网:http://automapper.org/ 源码:https://github.com/AutoMapper/AutoMapper NUGET安装: PM&g ...
- GO语言总结(5)——类型转换和类型断言
上一篇博客介绍了Go语言的数组和切片——GO语言总结(4)——映射(Map),本篇博客介绍Go语言的类型转换和类型断言 由于Go语言不允许隐式类型转换.而类型转换和类型断言的本质,就是把一个类型转换到 ...
- [LeetCode] Arithmetic Slices 算数切片
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...