Not getting triggered on RxMoya

203 views Asked by At

Could someone tell me why the first code gets triggered and the second code does not get triggered? (Assume "someEndPoint" is working properly)

let provider = MoyaProvider<MyApiService>()
provider.rx.request(.someEndPoint).subscribe(onSuccess: { response in
  print("triggered")
}, onError: { error in
}).disposed(by: disposeBag)
MoyaProvider<MyApiService>().rx.request(.someEndPoint).subscribe(onSuccess: { response in
  print("not triggered")
}, onError: { error in
}).disposed(by: disposeBag)

The difference between two codes is just to use "let" or not. I could not make "not triggered" printed in the second code and I can't see any network logs for the second code. I really want to know why it is.

Thanks in advance.

1

There are 1 answers

0
Daniel T. On BEST ANSWER

In the first example, you are creating and retaining the provider.

In the second example, you are creating the provider, then calling request but you aren't retaining the provider so it is deallocated immediately after the request is made and long before the server satisfies the request.

It takes a while for the network request to come back, and the provider needs to stay in existence until it does.