RX java组合逻辑

时间:2017-07-29 22:14:49

标签: rx-java reactive-programming rx-android

我是新手,因此这可能是一个愚蠢的问题。 我试图在一个方法调用中一个接一个地组合两个操作。

第1步是获取数据的一些细节并存储这些细节

第2步是根据这些细节获取数据。

public Observable<ApiResponse> getData(String id, String token)
{
    //step 1
    apiInterface.fetchDataDetails(id)
            .map(new Func1<Map<String, String>, Void>()
            {
                @Override
                public Void call(Map<String, String> details)
                {
                    if (details != null && !details.isEmpty() &&
                            details.containsKey("version") && details.containsKey("count"))
                    {
                        // save details
                    } else
                        throw new RuntimeException("Details not present");
                    return null;
                }
            });



    //step 2 if step 1 is complete i.e data details are saved 
    //then bring data

    int dataCount = 5; // the count saved from fetched details
    List<Observable> dataCalls = new ArrayList<>();
    for (int i = 0; i < dataCount; i++)
 dataCalls.add(apiInterface.fetchData(token,"http://blahblahblah/data/"+i));

    return Observable.zip(dataCalls, new FuncN<ApiResponse>()
    {
        @Override
        public ApiResponse call(Object... args)
        {
            // read all the data from all the observable, 
            //combine with some logic 
            // and then return just one ApiResponse.
            return new ApiResponse();
        }
     });
}

我无法理解的是,我如何结合第1步和第2步。 仅当步骤1成功完成时才应执行步骤2.

我试着尝试这个。

public Observable<ApiResponse> getData(String id, String token)
{
     return Observable.create(new Observable.OnSubscribe<ApiResponse>()
      {
        @Override
        public void call(Subscriber<? super ApiResponse> subscriber)
        {
            apiInterface.fetchDataDetails(id)
                        .map(new Func1<Map<String, String>, Void>()
                        {
                            @Override
                            public Void call(Map<String, String> details)
                            {
                                if (details != null && !details.isEmpty() &&
                                        details.containsKey("version") && 
                                        details.containsKey("count"))
                                {
                                    // save details
                                } else
                                    throw new RuntimeException("Details not 
                                                             present");
                                return null;
                            }
                        }).doOnCompleted(new Action0()
                            {
                                @Override
                                public void call()
                                {
                                    int dataCount = 5; // the count saved 
                                                      //from fetched details
                                    List<Observable> dataCalls = new 
                                                         ArrayList<>();
                                    for (int i = 0; i < dataCount; i++)
                                    dataCalls.add(apiInterface
                                                 .fetchData(token,
                                        "http://blahblahblah/data/"+i));

                                    return Observable.zip(dataCalls, new 
                                     FuncN<ApiResponse>()
                                    {
                                    @Override
                                    public ApiResponse call(Object... args)
                                    {
                                        // read all the data from all the 
                                       //observable, combine with some logic 
                                        // and then return just one 
                                        //ApiResponse.
                                        return new ApiResponse();
                                    }
                                    });
                                }
                            });
            }
       });
    }

但是这样我就无法发送我的数据,这感觉就像一个反模式。

1 个答案:

答案 0 :(得分:0)

我在这个问题上找到了答案

enter image description here