如果有人能想到另一种方式来实现我想要做的事情,欢迎你的想法!
为了给你一个想法,我有超过40种不同的功能,其签名类似于这些:
func getXFromServer(arg1: String, arg2: Int, arg3: Bool, completion: ((MyCustomResponse)->Void)? = nil)
func getYDataFromServer(completion: @escaping (Bool)->Void)
func getZDataFromServer(arg1: Int, completion: @escaping (MyGeneric<MyClass>)->Bool)
有些人有争论,有些则没有;有些是完成处理程序,有些是可选的。传递各种类型。
我正在尝试简化处理服务器响应的过程:目前它有很多重复的代码,所有这些都做了很多相同的事情,只是使用不同的参数值和类型。
因为这些函数都是异步的,所以我使用完成处理程序来处理服务器数据。我希望所有'重复代码'都放在一个函数中,让我们称之为handleServerResponse
:根据服务器响应的值,我想做更多异步的东西,并能够重新执行相同的外部函数
例如:
func getYDataFromServer(completion: @escaping (Bool)->Void)
{
session.dataTask(with: URLRequest()) { data, response, error in
handleServerResponse(){ valid //I want to pass 'getYDataFromServer(completion: completion)' here so that 'handleServerResponse' can determine whether to re-execute it or not, based on the server response
//do more stuff with Y server data
}
}.resume()
}
我目前正在处理handleServerResponse
之外的重新执行,如下所示:
func getYDataFromServer(completion: @escaping (Bool)->Void)
{
session.dataTask(with: URLRequest()) { data, response, error in
handleServerResponse(){ valid
if !valid
{
self.getXYZDataFromServer(){
getYDataFromServer(completion: completion)
}
return
}
//do more stuff with Y server data
}
}.resume()
}
这是一个非常简单的代码版本,但正如您所看到的,它可以非常快速地获得大量重复代码。
谢谢
也许这很有帮助
func getData(_ args: Any..., completion: @escaping ((Any)->Any)){
print(args)
completion(11)
}
但是函数类型不能强制对Any,你可以考虑使用enum来混合安全类型。
var valid: Bool = false
func getXFromServer(arg1: String, arg2: Int, arg3: Bool, completion: ((String)->Void)? = nil){}
func getYDataFromServer(completion: @escaping (Bool)->Void){
// session.dataTask(with: URLRequest()) { data, response, error in
// handleServerResponse(){ valid
valid.toggle()
print(valid)
if valid
{
MixGetData.XYZ.run{ MixCompletion.XYZ{ firstY.run{MixCompletion.Y(completion) } }}
}
else {
completion(false)
}
// }
// }.resume()
}
func getZDataFromServer(arg1: Int, completion: @escaping (String)->Bool){}
func getXYZDataFromServer(completion: @escaping ()->Void){
completion()
}
enum MixCompletion{
case X(((String)->Void)?)
case Y((Bool)->Void)
case Z((String)->Bool)
case XYZ(()->Void)
}
enum MixGetData{
case X( String, Int, Bool )
case Y
case Z(Int)
case XYZ
func run(completion: (() -> MixCompletion)? = nil){
if (completion == nil) {
switch (self) {
case let .X(arg1, arg2, arg3) : getXFromServer(arg1: arg1, arg2: arg2, arg3: arg3, completion : nil)
case let .Z(arg1) : getZDataFromServer(arg1: arg1, completion: {_ in return false})
case .Y : getYDataFromServer(completion: {_ in})
case .XYZ : getXYZDataFromServer(completion: {})
}}
else {
switch (self, completion!()) {
case (let .X(arg1, arg2, arg3), let .X(comp)): getXFromServer(arg1: arg1, arg2: arg2, arg3: arg3, completion : comp)
case (let .Z(arg1), let .Z(comp) ) : getZDataFromServer(arg1: arg1, completion: comp)
case (.Y, let .Y(comp)) : getYDataFromServer(completion: comp)
case (.XYZ, let .XYZ(comp)) : getXYZDataFromServer(completion: comp)
default: break
}
}
}
}
let firstY = MixGetData.Y
firstY.run()
firstY.run{MixCompletion.Y{bool in print (bool)}}
另一种方法是使用泛型函数。您也可以将两者结合起来:
func getYDataFromServer(completion: @escaping (Bool)->Void){
// session.dataTask(with: URLRequest()) { data, response, error in
// handleServerResponse(){ valid
valid.toggle()
print(valid)
if valid
{
getData(name: "XYZ", array: "") { getData(name: "Y", array: "", completion: completion)}
}
else {
completion(false)
}
// }
// }.resume()
}
func getData<T>(name: String , array: Any... , completion: T ){
switch name {
case "Y":
getYDataFromServer(completion: completion as! (Bool)-> Void)
case "X":
let arg1 = array[0] as! String; let arg2 = array[1] as! Int; let arg3 = array[2] as! Bool
getXFromServer(arg1: arg1, arg2: arg2, arg3: arg3, completion: completion as? (String)-> Void)
case "Z":
let arg1 = array[0] as! Int
getZDataFromServer(arg1: arg1, completion: completion as! (String)-> Bool)
case "XYZ":
getXYZDataFromServer(completion: completion as! ()-> Void)
default:
break;
}
}
getData(name: "Y", array : "", completion: { bool in print (123) } as (Bool)-> Void )
如果输入的类型超过四种,我知道这很烦人。但这是编写安全代码的唯一方法。如果你有运气,请告诉我们。
如果您只需要完成结果,也许这就是您想要的。
func handleServerResponse<T, U>(_ alternative : (T) -> () , _ completion : @escaping (U) -> (), _ handler : U , _ terminator : () , _ resValid : Bool){
if resValid {
alternative( completion(handler) as! T )
}
else {
terminator
}
}
func getYDataFromServer(completion: @escaping (Bool)->Void){
response.toggle()
// session.dataTask(with: URLRequest()) { data, response, error in
handleServerResponse({(a) in getXYZDataFromServer {a
}}, { (a: @escaping (Bool)->Void) in getYDataFromServer(completion: a) }, completion, completion(true), response)
要么
handleServerResponse( { (a) in getXYZDataFromServer{a}} , { (a: @escaping (Bool)->Void) in getZDataFromServer(arg1: 1, completion: { (s) -> Bool in
a
return false
})}, completion, completion(true), response)
它对我有用。