SwiftiOS技术笔记收录iOS

Swift Moya

2015-12-23  本文已影响7768人  TommyYaphetS

网络层这一块用Alamofire,如同于在oc中用AFNetworking.但是,如果你直接使用的话,会使得各种网络请求操作分布很凌乱,所以我选择了巧神封装的YTKNetwork,很好用,有兴趣的可以看一下.当然你也可以自己组织封装.
这段代码就是LZ项目中的网络请求:

NSDictionary *parameterDic = @{kPageSizeKey:@"10",kCurPageKey:@"1",kLastIDKey:@"0"};
[[WCRequestDataManager sharedRequestDataManager] requestDataForNetWorkWithDataHandleType:WCProductListDataHandleType
    parameterDic:parameterDic
    completed:^(WCProductResultModel *resultModel) {}
    failure:^(NSString *msg) {}
];

那么Swift中推荐一下Moya,这是一个基于Alamofire的更高层网络请求封装抽象层.
整个Demo可以在这里下载到:MoyaTest
可以对比一下直接用Alamofire和用Moya请求样式:

        Alamofire.request(.GET, kRequestServerKey + "services/creditor/product/list/page/2/0/0").responseJSON {
            response in
            if let value = response.result.value {
                let result = Mapper<CommonInfo>().map(value)
                let dataList = Mapper<ProductModel>().mapArray(result?.data?["result"])
                print("Alamofire = \(dataList?[0].productDesc)") // Alamofire = Optional("gfhgfgfhgshgdsfdshgfshfgh")
            }
        }
        
        MoyaTest.sharedInstance.requestDataWithTarget(.productList(pageSize: 2, curpage: 0, lastID: 0), type: ProductModel.self, successClosure: { result in
                let dataList = Mapper<ProductModel>().mapArray(result["result"])
                print("Moya = \(dataList?[0].productDesc)") // Moya = Optional("gfhgfgfhgshgdsfdshgfshfgh")
            }) { errorMsg in
                print(errorMsg)
        }

可见,第二种隐藏了url,method,json解析等参数/操作,抽象出了一层通用的请求方法.(按理说Mapper<ProductModel>().mapArray(result["result"])不应该出现在回调的闭包中,返回的就应该是productList请求对应的model,否则type这个参数就没有意义了,这个梗会在下面说到)


看一下文档说明:

<font size="5" color="IndianRed">Targets</font>

使用Moya的第一步就是定义一个Target:通常是指一些符合TargetType protocolenum.然,你请求的其余部分都只根据这个Target而来.这个枚举用来定义你的网络请求API的行为action.

public enum RequestApi {
   //  UserApi
   case login(loginName: String, password: String)
   case register //(userMobile: String, password: String, inviteCode: String, verifyCode: String)
   //case accountInfo

   //  ProductApi
   case productList(pageSize: Int, curpage: Int, lastID: Int)
//    case productDetail(id: Int)
}
extension RequestApi: TargetType {
   public var baseURL: NSURL {
       return NSURL(string: "http://apptest.wecube.com:8080/taojinjia/")!
   }
   
   public var path: String {
       switch self {
           case .login(_,_):
               return "services/crane/sso/login/doLogin"
           case .register:
               return "services/crane/sso/login/register"
           case let .productList(pageSize, curpage, lastID):
               return "services/creditor/product/list/page/"+String(pageSize)+"/"+String(curpage)+"/"+String(lastID)
       }
   }
   
   public var method: Moya.Method {
       switch self {
           case .login(_,_), .register:
               return .POST
           case .productList(_,_,_):
               return .GET
       }
   }
   
   public var parameters: [String: AnyObject]? {
       switch self {
           case let .login(loginName, password):
               return ["loginName": loginName, "userPassword": password]
           default :
               return nil
       }
   }
   
   //  单元测试用
   public var sampleData: NSData {
       return "{}".dataUsingEncoding(NSUTF8StringEncoding)!
   }
}

定义的enum实现TargetType协议,完成一系列初始化设置:

   参见源码,最终的url就是由baseURL和path拼接而来
   public final class func DefaultEndpointMapping(target: Target) -> Endpoint<Target> {
       let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString
       return Endpoint(URL: url, sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
   }

<font size="5" color="IndianRed">ProvidersEndpoints</font>

providerendpoints是紧密相关的,放在一起讲更好点(名字都怪怪的,果然国外开发者取名都是讲究哇)

let requestProvider = RxMoyaProvider<RequestApi>()

最终的请求发起对象就是requestProvider,RxMoyaProviderMoyaProvider的子类,你需要在podfile中导入Moya/RxSwift,当然你也可以直接用MoyaProvider来完成初始化,RxSwift目前只是简单的了解了一下,具体用法这里暂时忽略,不影响请求的完成.
你可能发现,这跟endpoints并没什么关系,但是,看下源码:

    /// Initializes a provider.
    public init(endpointClosure: EndpointClosure = MoyaProvider.DefaultEndpointMapping,
        requestClosure: RequestClosure = MoyaProvider.DefaultRequestMapping,
        stubClosure: StubClosure = MoyaProvider.NeverStub,
        manager: Manager = Alamofire.Manager.sharedInstance,
        plugins: [PluginType] = []) {
            
            self.endpointClosure = endpointClosure
            self.requestClosure = requestClosure
            self.stubClosure = stubClosure
            self.manager = manager
            self.plugins = plugins
    }

    /// Mark: Defaults

public extension MoyaProvider {
    
    // These functions are default mappings to endpoings and requests.
    
    public final class func DefaultEndpointMapping(target: Target) -> Endpoint<Target> {
        let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString
        return Endpoint(URL: url, sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
    }
    
    public final class func DefaultRequestMapping(endpoint: Endpoint<Target>, closure: NSURLRequest -> Void) {
        return closure(endpoint.urlRequest)
    }
}

<font size="5" color="IndianRed">Request</font>

import Foundation
import Moya
import RxSwift
import ObjectMapper
import SwiftyJSON

typealias SuccessClosure = (result: AnyObject) -> Void
//typealias SuccessClosure = (result: Mappable) -> Void
typealias FailClosure = (errorMsg: String?) -> Void

enum RequestCode: String {
    case failError = "0"
    case success = "1"
}

class MoyaTest {
    static let sharedInstance = MoyaTest()
    private init(){}
    
    let requestProvider = RxMoyaProvider<RequestApi>()
    
    func requestDataWithTarget<T: Mappable>(target: RequestApi, type: T.Type , successClosure: SuccessClosure, failClosure: FailClosure) {
        let _ = requestProvider.request(target).subscribe { (event) -> Void in
            switch event {
            case .Next(let response):
                let info = Mapper<CommonInfo>().map(JSON(data: response.data,options: .AllowFragments).object)
                guard info?.code == RequestCode.success.rawValue else {
                    failClosure(errorMsg: info?.msg)
                    return
                }
                guard let data = info?.data else {
                    failClosure(errorMsg: "数据为空")
                    return
                }
                successClosure(result: data)
            case .Error(let error):
                print("网络请求失败...\(error)")
            default:
                break
            }
        }
    }
}

最后的请求方法封装,如上面的栗子:

// 服务器给的数据格式统一为
{
   "code" = "",
   "data" =  {} 或 ({}),
   "msg" = ""
}

data对应的就是请求url返回的model[model],那么就是不是调用successClosure(result: data)了,而是

               //typealias SuccessClosure = (result: Mappable) -> Void
                let model = Mapper<T>().map(data)
               successClosure(result: model)

有的接口data对应的是包含了多个dic的数组,感觉解决方法就是再单独开一个数组的请求方法,调用mapArray,这里就不多加描述了,反正都一样的流程.
productList的url返回的data里面还包了一层resultpageVO,so...这就是一个特殊情况_!

ok!差不多Moya的基本使用就是这样啦,感觉还是非常方便实用的.

参考资料
通过 Moya+RxSwift+Argo 完成网络请求
RxSwift

上一篇 下一篇

猜你喜欢

热点阅读