Godfather Godfather - 2 months ago 76
Swift Question

RxSwift toArray() not subscribing

When i add toArray() before subscribing i get no callback.

googleCalendarUseCase.getEventsFromCalendars(calendars: selectedCalendars).subscribe(onNext: { (event) in
print(event.summary) //print thousands of elements
}).addDisposableTo(disposeBag)
googleCalendarUseCase.getEventsFromCalendars(calendars: selectedCalendars).toArray().subscribe(onNext: { (events) in
print(events.count) // Never gets called
}).addDisposableTo(disposeBag)


Maybe the problem is with the function getEventsFromCalendar but unsure why it works if i dont do toArray():

func getEventsFromCalendars(calendars: [GoogleCalendar.Calendar], nextPageToken: String? = nil) -> Observable<GoogleCalendar.Event> {
return Observable<GoogleCalendar.Event>.create { observer -> Disposable in
var parameters: [String: Any] = [:]
if let nextPageToken = nextPageToken {
parameters["pageToken"] = nextPageToken
}
_ = self.oauthswift.client.get(GoogleCalendarAPI.events, parameters: parameters, success: { (data, response) in
if let json = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
if let nextPageToken = self.nextPageToken(json: json) {
_ = Observable.of(Observable.from(self.getEventsFromJSON(json: json)), self.getEventsFromCalendars(calendars: calendars, nextPageToken: nextPageToken))
.merge().subscribe(observer)
} else {
_ = Observable.from(self.getEventsFromJSON(json: json))
}
} else {
observer.onError(CustomError.other)
}}, failure: { (error) in observer.onError(CustomError.noInet) }
)
return Disposables.create()
}
}

Answer

Use a debug() to check and ensure that you're getting a Completed event (without toArray()). toArray() will only emit an Array once the source sequence completes.

Comments