什么时候不适合使用Rxjava

原文:http://tomstechnicalblog.blogspot.hk/2016/07/when-not-to-use-rxjava.html
中文:http://blog.chengyunfeng.com/?p=1009

When Not to Use RxJava

Reactive programming is a game-changing technology. If you are using it correctly, it should change how you approach programming entirely. Over a year ago, I researched it hoping to find a better way to manage and compose UI events (and consequently took ownership of RxJavaFX). But I quickly learned it accomplishes muchmore than that. It changes the approach to almost every aspect of programming, from concurrency and IO to logic and algorithms.

In my enthusiasm, I started using RxJava for pretty much everything. Taking this ground-up approach to reactive programming greatly benefited the quality of my applications. For me, making everything reactive was probably the most effective way to learn RxJava too.

But after a year, I did find a few cases where reactive may not necessarily be a good fit. Every application I write now will be reactive. However, there may be a few places in the codebase I strategically choose to not make reactive. This might have partially been due to my switch to Kotlin, which made functional programming convenient whether it is push-based or pull-based, making Rx only one of several functional tools in my belt. But I digress. This article is merely my observations when using an RxJava Observable might not be optimal.

Keep in mind it is easy to turn pretty much anything into an Observable, including collections. Therefore, this post is about when it is appropriate to allow non-Observable items to be returned from your API. The clients of the API can always turn these items into Observables if they choose.

Case #1 Small, Constant, and Unchanging Data Sets

Here is the simplest case where an Observable might not be appropriate. Let's say you have a simple enum type for three EmployeeType categories.

public enum EmployeeType {
    FULL_TIME,
    CONTRACTOR,
    INTERN
}

If you need to iterate through this enumerable, does it always make sense to turn it into an Observable for the sake of?

Observable<EmployeeType> employeeTypes = Observable.from(Employee.values());

Perhaps it might make sense to turn EmployeeType into an Observable if you are already deep in an Observable chain of operators, and it simply follows the push-based flow to turn it into an Observable. But I would argue this is the exception rather than the norm.

Data sets that are small and do not change are probably not good candidates to turn into an Observable. From an API perspective, leave it as a traditional collection and allow it to be converted to an Observable when it makes sense. This does not apply to just enumerables, and perhaps this was an extreme example. But any small, static data set could fall into this case.

Case #2 Expensive, Cached Objects

Let's say you have a class called ActionQualifier which has some expensive regular expression fields. In case you didn't know, regular expressions are text wildcards on steroids. They are very powerful in finding text patterns but they are very expensive to compile at runtime. So creating an instance of this ActionQualifier could be very costly on performance if done redundantly.

public final class ActionQualifier {

    private final Pattern codeRegexPattern;
    private final int actionNumber;

    ActionQualifier(String codeRegex, int actionNumber) {
        this.codeRegexPattern = Pattern.compile(codeRegex);
        this.actionNumber = actionNumber;
    }

    public boolean qualify(String code) {
        return codeRegexPattern.matcher(code).find();
    }
    public int getActionCode() {
        return actionNumber;
    }
}

If you had an Observable that imported ActionQualifier instances from a database using RxJava-JDBC, it could be very expensive if it has several subscribers (since every subscription results in a re-query). For every subscription, it would rebuild ALL of them each time.

Observable<ActionQualifier> actionQualifiers = db
    .select("SELECT CODE_REGEX, ACTION_NUMBER FROM ACTION_MAPPING")
    .get(rs -> new ActionQualifier(rs.getString("CODE_REGEX"), rs.getInt("ACTION_NUMBER")));

You could use a cache() operator to hold onto them and "replay" them to each subscriber, and this is valid. But the actionQualifiers may become stale as cache()would hold onto them indefinitely.

 Observable<ActionQualifier> actionQualifiers = db
    .select("SELECT CODE_REGEX, ACTION_NUMBER FROM ACTION_MAPPING")
    .get(rs -> new ActionQualifier(rs.getString("CODE_REGEX"), rs.getInt("ACTION_NUMBER")))
    .cache();

Dave Moten has created a clever solution that expires the cache and re-subscribes to the source. But ultimately you got to ask if it is easier to simply hold the actionQualifiers in a List, and refresh them in a manual way. You might as well not be bound to a monad anymore.

List<ActionQualifier> actionQualifiers = db
    .select("SELECT CODE_REGEX, ACTION_NUMBER FROM ACTION_MAPPING")
    .get(rs -> new ActionQualifier(rs.getString("CODE_REGEX"), rs.getInt("ACTION_NUMBER")))
    .toList().toBlocking().first();

Then you can always turn the saved List into an Observable at any time, if you in fact want to use it as an Observable.

 Observable.from(actionQualifiers).filter(aq -> aq.qualify("TXB.*"));

Either way, collections of expensive items can be challenging to work with. In some situations, it is easier to manage them statefully rather than functionally. You can probably get creative and find ways to maintain a reactive nature depending on your situation, but be mindful to not over-complicate it. However, if you have a very large, memory-intensive collection then maybe an Observable is valid to prevent caching from taking up memory. It really depends on what "expensive" means.

Case #3 Simple "Lookups" and Single-Step Monads

What is great about RxJava is its ability to compose multiple steps. Take these, then filter that, map to this, and reduce to that. You can create a long, elaborate chain of operations doing tons of work with little code.

Observable<Product> products = ...

Observable<Int> totalSoldUnits = products
    .filter(pd -> pd.getCategoryId() == 3)
    .map(pd -> pd.getSoldUnits())
    .reduce(0,(x,y) -> x + y)

But what if you are only interested in doing one step, like looking up a single value with an ID?

Observable<Category> category = Category.forId(263);

Is an Observable overkill in this case? Maybe. It might just be simpler to return the Category without being emitted through an Observable.

Category category = Category.forId(263);

Maybe an Observable is warranted if you expect more than one Category to be emitted, or you want to return an empty Observable rather than a null value if no Category is found for that ID. But as we will discover next in Case #4, this excessive use of Observable might create more boilerplate code rather than reduce it. But for now note if you are expecting a single value that is simply a "look up", or it requires only one step, consider not using an Observable.

Besides, if you really want to utilize an Observable for a given usage, you can always convert it to one later. You can even filter out any null value to make it empty.

Observable<Category> category = Observable.just(Category.forId(263))
    .filter(c -> c != null);

Case #4 Frequently Qualified Properties

Let's expand on Case #3 to make another point. Say you have this Product class.

public final class Product { 
    private final int id;
    private final String description;
    private final int categoryId;

    public Product(int id, String description, int categoryId) { 
        this.id = id;
        this.description = description;
        this.categoryId = categoryId;
    }
    public Observable<Category> getCategory() { 
        return Category.forId(categoryId);
    }
}

Notice the getCategory() method returns an Observable<Category>. If we frequently qualify on the Category, this could be pretty messy. Suppose each Category has a getGroup() returning an int, and we want to filter out an Observable<Product> for only products where the category's group is 5.

Observable<Product> products = ...

Observable<Product> productsInGroup5 = 
    products.flatMap(p -> p.getCategory().filter(c -> c.getGroup() == 5).map(p -> c));

For a simple task, this requires a lot of FlatMap Kung-Fu. We flatMap() each Product to its Category, then filter for Categories where the getGroup() is 5, and then map the Category back to the Product. If we take the Product class and make its getCategory() a non-Observable, this would be a lot simpler.

public Category getCategory() { 
    return Category.forId(categoryId);
}
Observable<Product> productsInGroup5 = 
    products.filter(p -> p.getCategory().getGroup() == 5);

In summary, if you have fields on a class that are frequently filtered/qualified on, you might want to consider not making it an Observable to avoid complexity. This is especially true if the property returns a single value and not a sequence of values.

Case #5 Capturing State

RxJava is very anti-state. This is a good thing. It allows us to compose a series of actions and behaviors rather than manually manage a series of states. This allows operations to be agnostic to threads, and you can compose concurrency at any time with ease into any Observable chain.

But I have noticed with complex business applications (which definitely benefit from reactive programming), you sometimes want to capture state especially when trying to gather context of how your algorithm came up with an action. This is critical for business reporting. The simplest example I can think of is holding on to a snapshot of history.

public final class PricePoint { 
    private final int id;
    private final int productId;
    private final BigDecimal price;
    private final ImmutableList<BigDecimal> historicalPricePoints;

    public PricePoint(int id, int productId, BigDecimal price) { 
        this.id = id;
        this.productId = productId;
        this.price = price;
        historicalPricePoints = HistoricalPricePoints.forProductId(productId);
    }
    public ImmutableList<BigDecimal> getHistoricalPricePoints() { 
        return historicalPricePoints;
    }
}

You could retrieve the historical price points reactively, and this is fine if the operation is not expensive.

public final class PricePoint { 
    private final int id;
    private final int productId;
    private final BigDecimal price;

    public PricePoint(int id, int productId, BigDecimal price) { 
        this.id = id;
        this.productId = productId;
        this.price = price;
    }
    public Observable<BigDecimal> getHistoricalPricePoints() { 
        return HistoricalPricePoints.forProductId(productId);
    }
}

But if it is expensive, this goes back to Case #2. Also, if you want to capture the historical price points at the time the PricePoint is constructed, an Observable might not be optimal either. You do in fact want to hold on to state, and an anti-state solution like RxJava might undermine this.

If you want to gather and retain different states for different properties so you can hold onto a context snapshot, you might want to use a traditional pull-based solution to build all that.

Summary

Reactive programming is definitely a game-changer and you should use it liberally. But be aware that RxJava tackles moderate to high complexity problems, and not necessarily simple ones. Some of these cases I identified above may be obvious to Rx veterans. But to newbies encouraged to build applications "reactive from the ground-up", it may not be so obvious.

Again, these are just my observations. Please comment below if you have any questions, additional cases, affirmations, disagreements, or thoughts.

Posted by Thomas Nield at <abbr class="published" itemprop="datePublished" title="2016-07-07T20:22:00-07:00" style="border: none;">8:22 PM</abbr>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,132评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,802评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,566评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,858评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,867评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,695评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,064评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,705评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,915评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,677评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,796评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,432评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,041评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,992评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,223评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,185评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,535评论 2 343