1

Trying out RxSwift and trying to convert my network calls. I can't seem to display my data in the view because im not sure how I convert my observable to something my views can use. Here's an example of my request:

class SomeService {

    let provider = Provider()
    func getData() -> Observable<[Object]?> { // Returns json
        return provider
            .request(.getSomething())
            .debug()
            .mapArrayOptional(type: Object.self) 
            // Using Moya_Modelmapper map each item in the array
    }

}

In my view controller I get the data:

 let data = Service.getData()
 print(data) ... <Swift.Optional<Swift.Array<MyApp.Item>>>

I have tried to subscribe to the response to the sequence but I don't know how I actually convert it to something like an array I can use in my view.

UPDATE: With answer implemented:

    func itemsObserver() {
        print("Time to print step 1") // This gets printed
        data
        .filter { $0 != nil }.map { $0! }
        .subscribe(
            onNext: { objects in
                print(objects as Any) 
                print("Step 2") // This does not get executed at all
            },
            onCompleted:{ objects in
                print(objects as Any) // This is ()
                print("Complete") // This gets printed
            }
            ).addDisposableTo(disposeBag)
    }
    itemsObserver()

Console output:

 Time to print step 1
 Service.swift:21 (getData()) -> subscribed
 Service.swift:21 (getData()) -> Event next(Status Code: 200, Data Length: 141)
 Service.swift:21 (getData()) -> Event completed
 Service.swift:21 (getData()) -> isDisposed
 ()
 Complete
2
  • Your getData method seems infinitely recursive. I assume that isn't intentional. Maybe you could post some more info about the code? For example, the interface of this Provider class... Commented Apr 14, 2017 at 0:43
  • Ah sorry for the confusion the method in the provider is called something else in reality, so getData has a method called getSomething. My bad :) Commented Apr 18, 2017 at 8:54

1 Answer 1

1

Update:

If your onNext block isn't getting called at all, it's because data never produced anything. Either your producer isn't producing any objects or mapArrayOptional is not transforming them.

The onCompleted block doesn't accept any arguments so the objects var you have in it is meaningless/Void.


Try this:

let data = service.getData()
data
    .filter { $0 != nil }.map { $0! } // this removes the optionality of the result.
    .subscribe(onNext: { objects in
        // in here `objects` will be an array of the objects that came through.
}).disposed(by: bag)
Sign up to request clarification or add additional context in comments.

5 Comments

Not sure what's going on here: service.swift:21 (getData()) -> subscribed service.swift:21 (getData()) -> Event next(Status Code: 200, Data Length: 141) service.swift:21 (getData()) -> Event completed service.swift:21 (getData()) -> isDisposed onNext: { objects in print(objects as Any) // nil print("Step 2") // Step 2" }, onCompleted:{ objects in print(objects as Any) // () print("Complete") // Complete }
So my request shows my in the subscription that onNext is returning data but when I'm trying to print out objects in the onNext method it's nil?
The code in my answer won't allow objects to be nil. I think you need to add more code to your question.
I added your code snippet in the question and comments on what happens. My on next function never fires. @Daniel-T
I think you're right on the fact that the mapArrayOptional is not transforming my content. Which is a completely different question all together. But thank you so much for your help. Will mark this as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.