更新Moya/RxSwift中断我的网络通话 [英] Updating Moya/RxSwift breaking my network calls
问题描述
将我的iOS应用从Moya 8更新到11,我在网络实用工具类中具有以下功能,该工具被称为从服务器获取数据并将其映射以供其他类使用:
Updating my iOS app from Moya 8 to 11 and I have the following function in a network utility class which is called to get data from a server and map it for use in other classes:
func getServerData(endpoint: MyMoyaService) -> Observable<Any> {
let provider = RxMoyaProvider<MyMoyaService>()
return provider.request(endpoint)
.filterSuccessfulStatusAndRedirectCodes()
.mapJSON()
.flatMap { response -> Observable<Any> in
return Observable(…)
}
}
在更新后,RxMoyaProvider
被替换为MoyaProvider
,而您应该使用.rx
来使用RxSwift
扩展名.现在,这将返回Single
而不是Observable
,因此为了避免编译器错误,我还需要插入asObservable()
With the updates, RxMoyaProvider
was replaced with MoyaProvider
and instead you're supposed to use .rx
to use the RxSwift
extensions. This now returns a Single
instead of an Observable
so in order to avoid compiler errors I also needed to insert asObservable()
func getServerData(endpoint: MyMoyaService) -> Observable<Any> {
let provider = MoyaProvider<MyMoyaService>()
return provider.rx.request(endpoint)
.asObservable()
.filterSuccessfulStatusAndRedirectCodes()
.mapJSON()
.flatMap { response -> Observable<Any> in
return Observable(…)
}
}
但是flatMap
部分根本没有被调用,并且我的网络调用不再起作用.如果我在没有RxSwift的情况下执行此操作,只需打开结果,它就可以正常工作(但与我当前的设置不符),因此我知道网络调用和对自定义TargetType
服务的更新均已正确完成./p>
But the flatMap
portion is not being called at all and my network calls don't work anymore. If I do this without RxSwift, just switching on the result, it works OK (but that doesn't fit with my current setup) so I know that the network call and the updates to the custom TargetType
service are done correctly.
- 如何重新进行此操作(理想情况下,无需完全更改整个设置,并且
- (以示感谢),我如何利用
Single
而不是Observable
充分利用更新的设置(因为我假设通过使用asObservable
,我只是错过了有用的功能/更简洁的代码)
- How do I get this working again, (ideally without completely changing the entire setup, and
- (for extra credit) how do I take full advantage of the updated setup with
Single
instead ofObservable
(since I assume that by usingasObservable
I'm just missing out on useful features / cleaner code)
推荐答案
似乎问题在于保留了MoyaProvider
对象……如果我将提供程序移到函数外部并将其作为属性存储在类中,则一切正常正确地.
Seems the issue is retaining the MoyaProvider
object… If I move the provider outside of the function and store it as a property in the class, then everything works properly.
class MyClass {
let provider = MoyaProvider<MyMoyaService>()
func getServerData(endpoint: MyMoyaService) -> Observable<Any> {
return provider.rx.request(endpoint)
.asObservable()
.filterSuccessfulStatusAndRedirectCodes()
.mapJSON()
.flatMap { response -> Observable<Any> in
return Observable(…)
}
}
}
请始终记住保留您的提供商,因为如果您这样做,他们将被释放.
Always remember to retain your providers, as they will get deallocated if you fail to do so.
我想他们在某个时候更新了这些提供者的分配方式?
I suppose at some point they updated how these providers get deallocated?
这篇关于更新Moya/RxSwift中断我的网络通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!