1

I have this message in console because Api method called multiple times.

curl -X GET 
"https://newsapi.org/v2/top-headlines?apiKey=.............&pageSize=20&category=science&page=2" -i -v
Canceled (33ms)
2018-03-25 12:47:38.810618+0300 TestNewsApp[19773:2413537] Task <4B122FB1-1F0D-41FF-A102-BE2DE95BCBFC>.<42> finished with error - code: -999
2018-03-25 12:47:38.810779+0300 TestNewsApp[19773:2414036] Task <4B122FB1-1F0D-41FF-A102-BE2DE95BCBFC>.<42> HTTP load failed (error code: -999 [1:89])
curl -X GET 
"https://newsapi.org/v2/top-headlines?apiKey=.............&pageSize=20&category=science&page=2" -i -v
Canceled (34ms)
2018-03-25 12:47:38.844640+0300 TestNewsApp[19773:2413537] Task <9D6193D7-B757-4CB3-B812-452FE0B382BF>.<43> finished with error - code: -999
2018-03-25 12:47:38.844870+0300 TestNewsApp[19773:2413546] Task <9D6193D7-B757-4CB3-B812-452FE0B382BF>.<43> HTTP load failed (error code: -999 [1:89])
curl -X GET 
"https://newsapi.org/v2/top-headlines?apiKey=.............&pageSize=20&category=science&page=2" -i -v
Canceled (49ms)
2018-03-25 12:47:38.893744+0300 TestNewsApp[19773:2413579] Task <8A61A548-7F58-43CE-A829-57C8A6F952E5>.<44> finished with error - code: -999
2018-03-25 12:47:38.893937+0300 TestNewsApp[19773:2413546] Task <8A61A548-7F58-43CE-A829-57C8A6F952E5>.<44> HTTP load failed (error code: -999 [1:89])
curl -X GET 
"https://newsapi.org/v2/top-headlines?apiKey=.............&pageSize=20&category=science&page=2" -i -v
Success (1220ms): Status 200

Problem is in this code.

    scrollViewDidReachBottom
        .asObservable()
        .withLatestFrom(loadedPage.asObservable())
        .observeOn(MainScheduler.asyncInstance)
        .flatMapLatest { page in
            return NewsAPI.fetchNews(page: page + 1)
        }.subscribe( onNext: { [weak self]result in
            guard let strongSelf = self else { return }
            strongSelf.loadedPage.value = strongSelf.loadedPage.value + 1
            strongSelf.news.value = strongSelf.news.value + result
        })
        .disposed(by: disposeBag)

fetchNews(page: Int) -> Observable<[News]> is static func

I fix it with this code, but I'm not sure it's the correct way.

    scrollViewDidReachBottom
        .asObservable()
        .observeOn(MainScheduler.asyncInstance)
        .subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return }
            if !strongSelf.inCall {
                strongSelf.inCall = true
                NewsAPI.fetchNews(page: strongSelf.loadedPage.value + 1)
                    .observeOn(MainScheduler.asyncInstance)
                    .subscribe(onNext: { [weak self] news in
                        guard let strongSelf = self else { return }
                        strongSelf.news.value += news
                        strongSelf.loadedPage.value += 1
                        strongSelf.inCall = false
                    })
                    .disposed(by: strongSelf.disposeBag)
            }
        })
        .disposed(by: disposeBag)

2 Answers2

1

I fixed it with this code.

    scrollViewDidReachBottom
        .asObservable()
        .withLatestFrom(loadedPage.asObservable())
        .observeOn(MainScheduler.asyncInstance)
        .flatMapFirst { page in
            return NewsAPI.fetchNews(page: page + 1)
        }
        .subscribe( onNext: { [weak self]result in
            guard let strongSelf = self else { return }
            strongSelf.loadedPage.value += 1
            strongSelf.news.value = strongSelf.news.value + result
        })
        .disposed(by: disposeBag)

flatMapFirst solved my problem.

0

You have error:

finished with error - code: -999

According to this answer, it probably happened because you have trouble with network manager.

pacification
  • 5,838
  • 4
  • 29
  • 51
  • I use 'URLSession.shared.rx.data(request: request)' this is a singleton. `static func fetchNews(page: Int) -> Observable<[News]> return URLSession.shared.rx.data(request: request)` – O. Shulzhenko Mar 25 '18 at 10:36
  • And I always have Success at the end. I think it could be because of the multiple requests. Or troubles with code above. Because a single request does not return "Canceled". – O. Shulzhenko Mar 25 '18 at 12:12
  • Can I fix my code for don't call 'NewsAPI.fetchNews(page: Int)' before previous call ended? – O. Shulzhenko Mar 25 '18 at 12:23
  • Hi, @O.Shulzhenko. Maybe the multiple requests is the problem. But maybe the `.observeOn(MainScheduler.asyncInstance)` is cause the problem too? Did you try remove that? – pacification Mar 26 '18 at 11:20