Observable returned from function never sends onNext - swift

I have and observable that never sends onNext if its returned by a function, but if i subscribe to it in the function that returns it, onNext is called.
class InfoViewModel {
func refreshPushToken() {
PushNotificationService.sharedInstance.pushToken!
.flatMapLatest { (pushToken: String) -> Observable<Result<User>> in
return UserService.registerPushToken(pushToken)
}
.subscribe { (event ) in
print(event)
}
.addDisposableTo(disposeBag)
}
}
struct UserService {
....
static func registerPushToken(_ pushToken: String) -> Observable<Result<User>> {
...
return self.postUser(user: user)
}
static fileprivate func postUser(user: User) -> Observable<Result<User>> {
let rxProvider: RxMoyaProvider<Backend> = RxMoyaProvider<Backend>(endpointClosure: Backend.endpointClosure)
return rxProvider.request(Backend.register(user: user))
.mapObject(type: User.self)
.map({ (user: User) -> Result<User> in
LogService.log(level: .debug, action: "postUser", message: "Posted user with success", parameters: ["user": user.deviceId])
return .success(user)
})
.catchError({ error -> Observable<Result<User>> in
LogService.log(level: .error, action: "postUser", message: "Error posting user", parameters: ["user": user.deviceId, "error": error.localizedDescription])
return Observable.just(.failure(error))
})
}
}
But if I do this
rxProvider.request(Backend.register(user: user))
...
.subscribe { (event ) in
print(event)
}
in the UserService, i will get a next event.
I have tried to use debug() on the observable in InfoViewModel, there is a subscription, i just never receive any events.

So i figured it out, I was creating the RxMoyaProvider inside the method, so as soon as i went out of the scope of the method, it was deallocated. Which means that when was subscribing to it, it could no longer create the request. The reason that this wouldn't fail is because of how the observable is created
open func request(_ token: Target) -> Observable<Response> {
// Creates an observable that starts a request each time it's subscribed to.
return Observable.create { [weak self] observer in
let cancellableToken = self?.request(token) { result in
switch result {
case let .success(response):
observer.onNext(response)
observer.onCompleted()
case let .failure(error):
observer.onError(error)
}
}
return Disposables.create {
cancellableToken?.cancel()
}
}
}
As you can see, the request is called upon subscription, but since self had been deallocated the request was never fired. And all i got back was an empty observable.

Related

Observable sequence called several times on retryWhen, but should call only once

I am trying to build RxSwift Auth token refresh service using following tutorial: https://www.donnywals.com/building-a-concurrency-proof-token-refresh-flow-in-combine/. However, I faced with issue, when user don't have an auth token and first refresh failed, but second refresh succeed, additional request is send, and after this (3-rd request) is completed, only then called main endpoint
So, what I see in network inspector:
request to refresh token (failed)
request to refresh token (succeed)
request to refresh token (succeed)
request to main endpoint (succeed)
But it should be:
request to refresh token (failed)
request to refresh token (succeed)
request to main endpoint (succeed)
I have following code for Authenticator
protocol AuthenticatorType {
func authenticate() -> Observable<Void>
func checkForValidAuthTokenOrRefresh(forceRefresh: Bool) -> Observable<Void>
}
extension AuthenticatorType {
func checkForValidAuthTokenOrRefresh(forceRefresh: Bool = false) -> Observable<Void> {
return checkForValidAuthTokenOrRefresh(forceRefresh: forceRefresh)
}
}
final class Authenticator<Provider: RxMoyaProviderType> where Provider.Target == AuthAPI {
private let provider: Provider
private let cookiesStorageProvider: CookiesStorageProviderType
private let queue = DispatchQueue(label: "Autenticator.\(UUID().uuidString)")
private var refreshInProgressObservable: Observable<Void>?
init(
provider: Provider,
cookiesStorageProvider: CookiesStorageProviderType
) {
self.provider = provider
self.cookiesStorageProvider = cookiesStorageProvider
}
func checkForValidAuthTokenOrRefresh(forceRefresh: Bool = false) -> Observable<Void> {
return queue.sync { [weak self] in
self?.getCurrentTokenOrRefreshIfNeeded(forceRefresh: forceRefresh) ?? .just(())
}
}
func authenticate() -> Observable<Void> {
provider.request(.authenticate(credentials: .defaultDebugAccount))
.map(LoginResponse.self)
.map { loginResponse in
guard loginResponse.login else {
throw AuthenticationError.loginRequired
}
}
.asObservable()
}
}
// MARK: - Helper methods
private extension Authenticator {
func getCurrentTokenOrRefreshIfNeeded(forceRefresh: Bool = false) -> Observable<Void> {
if let refreshInProgress = refreshInProgressObservable {
return refreshInProgress
}
if cookiesStorageProvider.isHaveValidAuthToken && !forceRefresh {
return .just(())
}
guard cookiesStorageProvider.isHaveValidRefreshToken else {
return .error(AuthenticationError.loginRequired)
}
let refreshInProgress = provider.request(.refreshToken)
.share()
.map { response in
guard response.statusCode != 401 else {
throw AuthenticationError.loginRequired
}
return response
}
.map(RefreshReponse.self)
.map { refreshResponse in
guard refreshResponse.refresh else {
throw AuthenticationError.loginRequired
}
}
.asObservable()
.do(
onNext: { [weak self] _ in self?.resetProgress() },
onError: { [weak self] _ in self?.resetProgress() }
)
refreshInProgressObservable = refreshInProgress
return refreshInProgress
}
func resetProgress() {
queue.sync { [weak self] in
self?.refreshInProgressObservable = nil
}
}
}
And thats how I refresh doing request (with logics to refresh token)
func request(_ token: Target, callbackQueue: DispatchQueue?) -> Observable<Response> {
authenticator.checkForValidAuthTokenOrRefresh()
.flatMapLatest { [weak self] res -> Observable<Response> in
self?.provider.request(token).asObservable() ?? .empty()
}
.map { response in
guard response.statusCode != 401 else {
throw AuthenticationError.loginRequired
}
return response
}
.retry { [weak self] error in
error.flatMap { error -> Observable<Void> in
guard let authError = error as? AuthenticationError, authError == .loginRequired else {
return .error(error)
}
return self?.authenticator.checkForValidAuthTokenOrRefresh(forceRefresh: true) ?? .never()
}
}
}
At first, I thought it was concurrency problem, I changed queue to NSLock, but it all was the same. Also I tried to use subscribe(on:) and observe(on:), thats also don't give any effect.
Maybe issue with do block, where I set refreshInProgressObservable to nil, because when I change onError, to afterError, I don't see third request to refresh token, but I also don't see any request to main endpoint.
I even tried to remove share(), but as you guess it don't help either.
Ah, and also I remember that 3-rd request fires instantly after second is completed, even if I add sleep in beginning of getCurrentTokenOrRefreshIfNeeded method. So that kinda strange
Edit
I tried another way to refresh token, using deferred block in Observable (inspired by Daniel tutorial).
Here is my code
final class NewProvider {
let authProvider: MoyaProvider<AuthAPI>
let apiProvider: MoyaProvider<AppAPI>
let refreshToken: Observable<Void>
init(authProvider: MoyaProvider<AuthAPI>, apiProvider: MoyaProvider<AppAPI>) {
self.authProvider = authProvider
self.apiProvider = apiProvider
refreshToken = authProvider.rx.request(.refreshToken)
.asObservable()
.share()
.map { _ in }
.catchAndReturn(())
}
func request(_ token: AppAPI) -> Observable<Response> {
Observable<Void>
.deferred {
if CookiesStorageProvider.isHaveValidAuthToken {
return .just(())
} else {
throw AuthenticationError.loginRequired
}
}
.flatMapLatest { [weak self] _ in
self?.apiProvider.rx.request(token).asObservable() ?? .never()
}
.retry { [weak self] error in
return error.flatMapLatest { [weak self] _ in
self?.refreshToken ?? .never()
}
}
}
}
It works perfectly for one request (like, "it sends request to refresh token only when auth token is missing and try to refresh token again if token refresh failed")
However, there is problem with multiple requests. If there is no auth token and multiple request are fired, it works well, requests are waiting for token to refresh. BUT, if token refresh failed, there is no attempt to try refresh token again. I don't know what can lead to this behaviour.
EDIT 2
I found out that if I place
.observe(on: SerialDispatchQueueScheduler(queue: queue, internalSerialQueueName: "test1"))
after
.share()
refreshToken = authProvider.rx.request(.refreshToken)
.asObservable()
.share()
.observe(on: SerialDispatchQueueScheduler(queue: queue, internalSerialQueueName: "test1"))
.map { _ in }
.catchAndReturn(())
All will be work as expected, but now I can't understand why its working this way
Okay, I pulled down your code and spent a good chunk of the day looking it over. A couple of review points:
This is way more complex than it needs to be for what it's doing.
Any time you have a var Observable, you are doing something wrong. Observables and Subjects should always be let.
There is no reason or need to use a DispatchQueue the way you did for Observables. This code doesn't need one at all, but even if it did, you should be passing in a Scheduler instead of using queues directly.
I could see no way for your code to actually use the new token in the retry once it has been received. Even if these tests did pass, the code still wouldn't work.
As far as this specific question is concerned. The fundamental problem is that you are calling getCurrentTokenOrRefreshIfNeeded(forceRefresh:) four times in the offending test and creating three refreshInProgress Observables. You are making three of them, because the second one has emitted a result and been disposed before the last call to the function is made. Each one emits a value so you end up with three next events in authAPIProviderMock.recordedEvents.
What is the fix? I could not find a fix without making major changes to the basic structure/architecture of the code. All I can do at this point is suggest that you check out my article on this subject RxSwift and Handling Invalid Tokens which contains working code for this use case and includes unit tests. Or revisit Donny's article which I presume works, but since there are no unit tests for his code, I can't be sure.
Edit
In answer to your question in the comments, here is how you would solve the problem using my service class:
First create a tokenAcquisitionService object. Since you don't actually need to pass a token value around, just use Void for the token type.
let service = TokenAcquisitionService(initialToken: (), getToken: { _ in URLSession.shared.rx.response(request: refreshTokenRequest) }, extractToken: { _ in })
(Use whatever you want in place of URLSession.shared.rx.response(request: refreshTokenRequest). The only requirement is that it returns an Observable<(response: HTTPURLResponse, data: Data)> and in this case the data can simply be Data() or anything else, since it is ignored. It can even present a view controller that asks the user to login.)
Now at the end of every request, include the following.
.do(onNext: { response in
guard response.response.statusCode != 401 else { throw TokenAcquisitionError.unauthorized }
})
.retry(when: { $0.renewToken(with: tokenAcquisitionService) })
Wrap the above however you want so you don't have to copy pasted it onto every request.
QED

RxSwift+Alamofire custom mapper error handling

RxSwift one more question about error handling:
I'm using Alamofire+RxAlamofire this way:
SessionManager.default.rx.responseJSON(.post, url, parameters:params)
example:
func login() -> Observable<Int> {
let urlString = ...
let params = ...
return SessionManager.default.rx.responseJSON(.post, url, parameters:params)
.rxJsonDefaultResponse()
.map({ (data) in
data["clientId"] as! Int
})
}
....
extension ObservableType where Element == (HTTPURLResponse, Any) {
func rxJsonDefaultResponse() -> Observable<Dictionary<String, Any>> {
return self.asObservable().map { data -> Dictionary<String, Any> in
if... //error chechings
throw NSError(domain: ..,
code: ...,
userInfo: ...)
}
...
return json
}
}
}
using:
loginBtn.rx.tap
.flatMap{ _ in
provider.login()
}.subscribe(onNext: { id in
...
}, onError: { (er) in
ErrorPresentationHelper.showErrorAlert(for: er)
})
.disposed(by: bag)
So if error occurred everything works as intended: error alert shows and 'loginBtn.rx.tap' disposed, but I need it to be still alive, what's my strategy here if I want to use onError block?
You can use materialize function in rxSwift. It will convert any Observable into an Observable of its events. So that you will be listening to Observable<Event<Int>> than Observable<Int>. Any error thrown from the flatmap would be captured as error event in your subscription block's onNext and can be handled there. And your subscription would still be alive. Sample code would be as follows.
button.rx.tap.flatMap { _ in
return Observable.just(0)
.flatMap { _ -> Observable<Int> in
provider.login()
}.materialize()
}.subscribe(onNext: { event in
switch event {
case .next:
if let value = event.element {
print(value) //You will be getting your value here
}
case .error:
if let error = event.error {
print(error.localizedDescription) //You will be getting your captured error here
}
case .completed:
print("Subscription completed")
}
}) {
print("Subscription disposed")
}.disposed(by: disposeBag)
Hope it helps. You can checkout the materialize extension here.

Refresh Observable in response to another

I have an observable that emits a list of CNContacts, and I want to reload the list when there is a change to the Contacts database (.CNContactStoreDidChange).
So the observable should emit a value on subscription, and whenever the other observable (the notification) emits a value. That sounds like combining them with withLatestFrom, but it doesn't emit anything.
let myContactKeys = [
CNContactIdentifierKey as CNKeyDescriptor,
CNContactFormatter.descriptorForRequiredKeys(for: .fullName)
]
func fetchContacts(by identifiers: [String],
contactKeys: [CNKeyDescriptor]) -> Observable<Event<[CNContact]>> {
return Observable<[String]>.just(identifiers)
.withLatestFrom(NotificationCenter.default.rx.notification(Notification.Name.CNContactStoreDidChange)) { ids, _ in ids}
.flatMap { ids in
Observable<[CNContact]>.create { observer in
let predicate = CNContact.predicateForContacts(withIdentifiers: ids)
do {
let contacts = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch: contactKeys)
observer.onNext(contacts)
} catch {
observer.onError(error)
}
return Disposables.create()
}
.materialize()
}
.observeOn(MainScheduler.instance)
.share(replay: 1)
.debug()
}
fetchContacts(by: ["123"], contactKeys: myContactKeys)
.subscribe(
onNext: { contacts in
contacts.forEach { print($0.fullName) }
},
onError: { error in
print(error.localizedDescription)
})
.dispose(by: disposeBag)
The problem with your code is that you are starting with Observable<[String]>.just(identifiers) which will emit your identifiers and immediately complete. You don't want it to complete, you want it to continue to emit values whenever the notification comes in.
From your description, it sounds like you want something like the below. It emits whenever the notification fires, and starts with the contacts.
let myContactKeys = [
CNContactIdentifierKey as CNKeyDescriptor,
CNContactFormatter.descriptorForRequiredKeys(for: .fullName)
]
func fetchContacts(by identifiers: [String], contactKeys: [CNKeyDescriptor]) -> Observable<Event<[CNContact]>> {
func update() throws -> [CNContact] {
let predicate = CNContact.predicateForContacts(withIdentifiers: identifiers)
return try CNContactStore().unifiedContacts(matching: predicate, keysToFetch: contactKeys)
}
return Observable.deferred {
NotificationCenter.default.rx.notification(Notification.Name.CNContactStoreDidChange)
.map { _ in }
.map(update)
.materialize()
}
.startWith({ () -> Event<[CNContact]> in
do {
return Event.next(try update())
}
catch {
return Event.error(error)
}
}())
.share(replay: 1)
.debug()
}

Why use the responseWith method?

In the process of reading the RXAlamofire source code, there is a place that I don't understand very well.
Since this method is an observable object for creating a DataRequest, why call the responseWith method?
func request<R: RxAlamofireRequest>(_ createRequest: #escaping (SessionManager) throws -> R) -> Observable<R> {
return Observable.create { observer -> Disposable in
let request: R
do {
request = try createRequest(self.base)
observer.on(.next(request))
request.responseWith(completionHandler: { response in
if let error = response.error {
observer.on(.error(error))
} else {
observer.on(.completed)
}
})
if !self.base.startRequestsImmediately {
request.resume()
}
return Disposables.create {
request.cancel()
}
} catch {
observer.on(.error(error))
return Disposables.create()
}
}
}
I believe the authors of RXAlamofire use this as their convention. If you look at there request implementation All of the request methods return the result of a method responseXYZ. The response methods typically execute the request and respond with something (JSON, String, etc.) Sounds a bit confusing but its kind of like this request some data respond with something.

How do you sequentially chain observables in concise and readable way

Im new to RXSwift and I've begun investigating how I can perform Promise like function chaining.
I think I'm on the right track by using flatmap but my implementation is very difficult to read so I suspect theres a better way to accomplish it.
What I have here seems to work but I'm getting a headache thinking about what It might looks like if I added another 3 or functions to the chain.
Here Is where I declare my 'promise chain'(hard to read)
LOGIN().flatMap{ (stuff) -> Observable<Int> in
return API(webSiteData: stuff).flatMap
{ (username) -> Observable<ProfileResult> in
return accessProfile(userDisplayName: username) }
}.subscribe(onNext: { event in
print("The Chain Completed")
print(event)
}, onError:{ error in
print("An error in the chain occurred")
})
These are the 3 sample functions I'm chaining
struct apicreds
{
let websocket:String
let token:String
}
typealias APIResult = String
typealias ProfileResult = Int
// FUNCTION 1
func LOGIN() -> Observable<apicreds> {
return Observable.create { observer in
print("IN LOGIn")
observer.onNext(apicreds(websocket: "the web socket", token: "the token"))
observer.on(.completed)
return Disposables.create()
}
}
// FUNCTION 2
func API(webSiteData: apicreds) -> Observable<APIResult> {
return Observable.create { observer in
print("IN API")
print (webSiteData)
// observer.onError(myerror.anError)
observer.on(.next("This is the user name")) // assiging "1" just as an example, you may ignore
observer.on(.completed)
return Disposables.create()
}
}
//FUNCTION 3
func accessProfile(userDisplayName:String) -> Observable<ProfileResult>
{
return Observable.create { observer in
// Place your second server access code
print("IN Profile")
print (userDisplayName)
observer.on(.next(200)) // 200 response from profile call
observer.on(.completed)
return Disposables.create()
}
}
This is a very common problem we run into while chaining operations. As a beginner I had written similar code using RxSwift in my projects as well. And there are two areas of improvement -
1. Refactor the code to remove nested flatMaps
2. Format it differently to make the sequence easier to follow
LOGIN()
.flatMap{ (stuff) -> Observable<APIResult> in
return API(webSiteData: stuff)
}.flatMap{ (username) -> Observable<ProfileResult> in
return accessProfile(userDisplayName: username)
}.subscribe(onNext: { event in
print("The Chain Completed")
print(event)
}, onError:{ error in
print("An error in the chain occurred")
})
In addition to nested flatMap and code formatting, you could omit return and explicit return types:
LOGIN()
.flatMap { webSiteData in API(webSiteData: webSiteData) }
parameter names
LOGIN()
.flatMap { API(webSiteData: $0) }
or even remove parameters at all where appropriate:
LOGIN()
.flatMap(API)
.flatMap(accessProfile)
.subscribe(
onNext: { event in
print(event)
}, onError:{ error in
print(error)
}
)
FYI there is Observable.just method which would be convenient here:
struct ApiCredentials {
let websocket: String
let token: String
}
func observeCredentials() -> Observable<ApiCredentials> {
let credentials = ApiCredentials(websocket: "the web socket", token: "the token")
return Observable.just(credentials)
}
Try to follow official Swift API Guidelines to make your code more readable.
You can also use the point-free style and just pass function references to flatMap:
LOGIN()
.flatMap(API)
.flatMap(accessProfile)
.subscribe(onNext: { event in
print("The Chain Completed")
print(event)
}, onError:{ error in
print("An error in the chain occurred")
})