ReactiveCocoa 5, ReactiveSwift for handling subqueries and best practices

I am trying to find best practice for handling multiple subqueries for each value received from a parent query. I am trying to use the same logic as here - Cocoa 5 reaction and ReactiveSwift to handle network requests , but there are some problems.

What we need:
1. TableView with an infinite scroll handler (SVPullToRefresh)
2. Selects a list of objects for each time handler. 3. Send a "subquery" for each object from the response

Notes:
1. All requests (parent + sub-requests) must be canceled after the viewController is closed (deinit called)
2. I need to be able to cancel the parent request at any time. This should also cancel all subpages.

I currently have

I know what I am doing in the "endless handler", it's a kind of sticky tape, but I'm new to ReactiveSwift ...

self.tableView.addInfiniteScrollingWithActionHandler { [unowned self] in
    self.tempMutableProperty.value = true
}

self.tempMutableProperty.producer.skipNil().flatMap(.latest) { [unowned self] tempValueThatIDontNeed in
    return self.producerForParentRequest(offset: self.offset)
        .take(during: self.reactive.lifetime)
        .on(
            // handlers for errors, completed, etc
            value: { [unowned self] items in
                self.items.append(items)
                self.tableView.reloadData()
                self.offset += items.count
                // SEND REQUEST #2 FOR EACH ITEM
            }
    ).flatMapError { error in
        return SignalProducer.empty
    }
}.observe(on: UIScheduler().start()

So, as you can see, I have pagination using tableView. I collect a list of objects for each page. Then, for each element from the answer, I need to get additional information with request No. 2.


:
1. , tempMutableProperty - parent request - - 2. sub-request , , , value/error sub-request , , 10 , 10 . , , 3. , . , , , , parent request sub-requests .
4. # 2, , . , parent request, sub-requests parent request 5.. self.deinit, self.lifetime, ,

, , / self, , sub-request - .


+4
2

, .

# 1 :

let disposable = SerialDisposable()

self.tableView.addInfiniteScrolling(actionHandler: { [unowned self] in
    self.disposable.inner = nil // this is needed to force dispose current request before starting new one
    self.disposable.inner = self.producer().take(during: self.reactive.lifetime)
        .on(value: { [unowned self] value in
            // handle as you want
        }).start()
})

tempMutableProperty. flatMap SerialDisposable. , , , self

:

, , , . , , 3 - item, itemDetail, requestSent.

willDisplay cell

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if !self.items[indexPath.row].requestSent {
        self.items[indexPath.row].details <~ self.detailedProducerForItemID(self.items[indexPath.row].item.id)
        self.items[indexPath.row].requestSent = true
    }
}

: self.items[indexPath.row].details MutableProperty<Details?>

- :

let (detailsSignal, detailsObserver) = Signal<MutableProperty<Details?>, NoError>.pipe(), Details - .

awakeFromNib:

let details = self.detailsSignal.flatMap(.latest) { $0.producer }

self.detailsLabel.reactive.text <~ details.map { value -> String? in
    // handling `Details`
}

cellForRow cell.detailsObserver.send(value: self.items[indexPath.row].details)

, VC deinit main, , self.items.removeAll() .

. - , .

0

, :

extension Reactive where Base: UITableView {
    public func infiniteScrollingSignal() -> Signal<Void, NoError>
    {
        return Signal { [unowned base = self.base] observer in
            base.addInfiniteScrollingWithActionHandler  {
                observer.send(value: ())
            }

            return ActionDisposable {
                // Unsubscribe the infinite scrolling action handler here if necessary
            }
        }
        .take(during: self.lifetime)
    }
}

self.tableView.reactive.infiniteScrollingSignal()

0

Source: https://habr.com/ru/post/1675635/


All Articles