RxJava结合了可观察量并与之前的值进行比较

时间:2017-04-14 22:40:58

标签: rx-java

我正在尝试找出按顺序组合不同类型的observable的最佳方法,然后比较结果。看看zip和combineLatest,我觉得它们不合适。我该怎么办?

@Test
    public void shouldInsertRecord(){



        BidsEntity bid = new BidsEntity(/* omitted */);


        Observable<Integer> count1 = bidsRepository.getBid(bid.getUserId(), bid.getListingId()).count();

        Observable<Boolean> insert = bidsRepository.insertBid(bid);

        Observable<Integer> count2 = bidsRepository.getBid(bid.getUserId(), bid.getListingId()).count();


        //run count1, run insert, run count2 in that order
        //assert that count2 is greater than count1 

    }

1 个答案:

答案 0 :(得分:0)

不是顺序(又称阻塞)是ReactiveX的重点。

看看你的例子,我认为只要插入一个Bid就有一个可以发出值的observable是合理的。然后你订阅那个observable并在一个观察者中做你想做的任何事情。

您已经拥有适合它的设置。让class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. collectionView?.delegate = self collectionView?.contentInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let x: CGFloat = 50 + CGFloat(arc4random_uniform(50)) // or 128.0, doesn't matter return CGSize(width: x, height: 28) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 15, left: 0, bottom: 15, right: 0) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 15 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 50 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { return collectionView.dequeueReusableCell(withReuseIdentifier: "aaa", for: indexPath) } override func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } 返回每当添加新Bid时发出的observable,并用单个观察者替换bidsRepository.getBid(...)个变量。