我在我的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
。该怎么做?
答案 0 :(得分: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
}
答案 1 :(得分:0)
尝试一下...
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()
})
答案 2 :(得分:0)
我已经遇到了这个问题,这是解决方案。
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
是带有静态方法的类,显示了我的自定义活动指示器。
祝你好运!