将任何活动指示器库与 Moya 一起使用
Posted
技术标签:
【中文标题】将任何活动指示器库与 Moya 一起使用【英文标题】:Use any Activity Indicator library with Moya 【发布时间】:2018-12-17 13:23:05 【问题描述】:我在我的 ios 项目中使用Moya Networking。我做了一个具有以下方法的类:
class UserAdapter
static func getUsers(amount:Int=2, gender:Gender = .Male, success successCallback: @escaping ([UserModelMain]) -> Void, error errorCallback: @escaping (Swift.Error) -> Void, failure failureCallback: @escaping (MoyaError) -> Void)
let endpointClosure = (target: UserTarget) -> Endpoint<UserTarget> in
let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
return defaultEndpoint.adding(newHTTPHeaderFields: ["": ""])
let provider = MoyaProvider<UserTarget>(endpointClosure: endpointClosure, plugins: [NetworkLoggerPlugin(verbose: true), NetworkActivityPlugin(networkActivityClosure: (activity, target) in
switch activity
case .began:
print("Network Activity began")
UIApplication.shared.isNetworkActivityIndicatorVisible = true
case .ended:
print("Network Activity ended")
UIApplication.shared.isNetworkActivityIndicatorVisible = false
)])
provider.request(.getUsers(amount: amount, gender: gender)) (result) in
switch result
case .success(let response):
//TODO
我从我的视图控制器调用这个:
UserAdapter.getUsers(amount: 8, gender: .Female, success: (response) in
, error: (error) in
// show error from server
, failure: (error) in
// show Moya error
)
我需要使用任何自定义加载库,例如 NVActivityIndicatorView,而不是 UIApplication.shared.isNetworkActivityIndicatorVisible
。如何做到这一点?
【问题讨论】:
您希望它能够与任何自定义库一起使用,还是希望它专门与`NVActivityIndicatorView? Also, it would be better to reuse
MoyaProvider`一起使用并在函数外部声明它。
任何加载器库
谢谢,我在函数外声明它为“静态让提供者”
我想使用任何加载器库,我不想让视图控制器处理我的加载器启动/停止,但是这个 UserAdapter 类来完成这项工作。问题是加载器视图显然在场景/视图控制器中。如何通过委托闭包或其他方式在 UserAdapter 类中处理它?
【参考方案1】:
static func getUsers(amount:Int=2,
gender:Gender = .Male,
success successCallback: @escaping ([UserModelMain]) -> Void,
error errorCallback: @escaping (Swift.Error) -> Void,
failure failureCallback: @escaping (Moya.MoyaError) -> Void,
progress progressCallback: ProgressBlock?,
activity networkActivityCallback: @escaping (_ change: NetworkActivityChangeType) -> Void)
let userProvider = MoyaProvider<UserTarget>(endpointClosure: (target: UserTarget) -> Endpoint<UserTarget> in
let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
return defaultEndpoint.adding(newHTTPHeaderFields: ["": ""])
, plugins: [NetworkLoggerPlugin(verbose: true), NetworkActivityPlugin(networkActivityClosure: (activity, target) in
UIApplication.shared.isNetworkActivityIndicatorVisible = (activity == .began)
networkActivityCallback(activity)
switch activity
case .began:
print("Network Activity began")
case .ended:
print("Network Activity ended")
)])
//Other statements
【讨论】:
【参考方案2】:试试这个...
print("Network Activity began")
Helper.showHUD()
UserAdapter.getUsers(amount: 8, gender: .Female, success: (response) in
print("Network Activity ended")
Helper.hideHUD()
, error: (error) in
// show error from server
print("Network Activity ended")
Helper.hideHUD()
, failure: (error) in
// show Moya error
print("Network Activity ended")
Helper.hideHUD()
)
【讨论】:
谢谢,但我的问题是如何将它添加到 UserAdapter 类。我不想让 viewController 处理加载器。 我想使用任何加载器库,我不想让视图控制器处理我的加载器启动/停止,但是这个 UserAdapter 类来完成这项工作。问题是加载器视图显然在场景/视图控制器中。如何通过委托闭包或其他方式在 UserAdapter 类中处理它? 【参考方案3】:从工作项目复制粘贴
我遇到过这个问题,这里是解决方案。
网络管理器
class HomeNetworkManager: HomeNetworkable
// MARK: Properties
private let provider: MoyaProvider<HomeAPI>
// MARK: Initiallization
init(provider: MoyaProvider<HomeAPI>)
self.provider = provider
convenience init()
let networkActivityClosure: NetworkActivityPlugin.NetworkActivityClosure = (activity, _) in
switch activity
case .began:
Activity.startAnimating()
case .ended:
Activity.stopAnimating()
var plugins : [PluginType] = []
let networkActivityPlugin = NetworkActivityPlugin(networkActivityClosure: networkActivityClosure)
plugins.append(networkActivityPlugin)
let networkLogger = NetworkLoggerPlugin(verbose: true)
plugins.append(networkLogger)
let provider = MoyaProvider<HomeAPI>(plugins: plugins)
self.init(provider: provider)
// MARK: Methods - Methods defined in HomeNetworkable Protocol
然后轻松使用它并免费获得活动指示器:
let homeNetworkManager = HomeNetworkManager()
注意:
Activity
是一个带有静态方法的类,显示我的自定义活动指示器。
祝你好运!
【讨论】:
以上是关于将任何活动指示器库与 Moya 一起使用的主要内容,如果未能解决你的问题,请参考以下文章