混合并匹配RxJava订阅线程

时间:2015-09-05 21:41:53

标签: rx-java rx-android

是否可以在RxJava中混合和匹配调度程序线程。基本上我想在Android上做类似以下的事情。

<?php
$result = $mysqli->query("SELECT COUNT(*) FROM webelements WHERE web='ALR' AND cat='Article'");
$total_results = $result->num_rows;
$result->close();

    /* Setup vars for query. */
    $targetpage = "moving-articles";
    $limit = 15;                                
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;  
    else
        $start = 0;         

    /* Get data. */
    $sql = $mysqli->query("SELECT * FROM webelements WHERE web='ALR' AND cat='Article' ORDER BY datestamp DESC LIMIT $start, $limit");

    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_results/$limit);    //lastpage  = total results / items per page, rounded up.
    if ($page == 0) $page = $lastpage;          //if no page var is given, default to last page (as first page)
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* Draw the pagination object.  */

    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "
        <div class='pagination'>";
        /* Next Button */
        if ($page < $lastpage) 
            $pagination.= "<a href='$targetpage/page/$next/'>« next</a>";
        else
            $pagination.= "<span class='disabled'>« next</span>";   

        /* Backwards Pagination */
        for ($counter = $lastpage; $counter > 0; $counter--) {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";
        }

        /* Previous Button */
        if ($page > $counter - 1) 
            $pagination.= "<a href='$targetpage/page/$prev/'>previous »</a>";
        else
            $pagination.= "<span class='disabled'>previous »</span>";

        $pagination.= "</div>";     
    }
?>

这可能在同一个订阅中吗?

1 个答案:

答案 0 :(得分:3)

是的,在您完全理解逻辑之后,这是可能的,而且非常简单。但是你可能会混淆一点observeOn()和subscribeOn()运算符:)

uiObservable
    .switchMap(o -> return anotherUIObservable)
    .subscribeOn(AndroidSchedulers.mainThread())  // means that the uiObservable and the switchMap above will run on the mainThread.

    .switchMap(o -> return networkObservable) //this will also run on the main thread

    .subscribeOn(Schedulers.newThread()) // this does nothing as the above subscribeOn will overwrite this

    .observeOn(AndroidSchedulers.mainThread()) // this means that the next operators (here only the subscribe will run on the mainThread
    .subscribe(result -> doSomething(result))

也许这就是你想要的:

uiObservable
    .switchMap(o -> return anotherUIObservable)
    .subscribeOn(AndroidSchedulers.mainThread()) // run the above on the main thread

    .observeOn(Schedulers.newThread())
    .switchMap(o -> return networkObservable) // run this on a new thread

    .observeOn(AndroidSchedulers.mainThread()) // run the subscribe on the mainThread
    .subscribe(result -> doSomething(result))

奖励:我已经写过关于这些运营商的a post,希望有所帮助