1

I made a few gateways / providers to integrate with an API in my API, using RX Swift and I'm trying to handle the pagination in what seems to me like a clean and simple way.

Basically, the function signature would look like that:

func getPlaces(with location: CLLocationCoordinate2D) -> Observable<(value: [Place], next: Observable<(value: [Places], Observable<(value: [Place], next: ... ... >>

This quickly appears impractical, so I tried creating a typealias for that:

typealias Result = Observable<(value: [Place], next: Result?)>

So my function signature would look like this:

func getPlaces(with location: CLLocationCoordinate2D) -> Result

But Xcode wouldn't get fooled so easily and calls me out for referencing my typealias inside itself

So... is it even doable ? How ?

0

2 Answers 2

7

I don't think this is possible using a typealias because you are creating an infinite type. The only way I can think of is to make Observable a recursive enumeration:

enum Observable {
   case end([Place])
   indirect case node([Place], Observable)
}
1
  • So, it works ! "Observable" is not appropriate, since it's an RxSwift type, but I mixed our approaches
    – ElFitz
    Mar 31, 2018 at 15:19
2

So, I mixed my approach with Nibr's, using a single case. This allows to handle pagination much more simply (in my opinion) from the ViewModel's side

enum Result<T> {
    indirect case node([T], Observable<Result>?)

    var value: [T] {
        if case let GatewayResult.node(value, _) = self {
            return value
        }
        return []
    }

    var next: Observable<Result>? {
        if case let GatewayResult.node(_, next) = self {
            return next
        }
        return nil
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.