网站被黑 百度跳转,朋友做的网站图片不显示不出来,3366网页游戏大全,广州互助网站开发【iOS Swift Moya 最新请求网络框架封装通用】 前言框架结构1.API定义#xff08;TargetType#xff09;2. 配置MoyaProvider3. 网络管理器4. 使用示例注意事项进一步优化 前言
设计一个基于Moya的网络请求框架#xff0c;可以提供灵活的网络请求管理#xff0c;例如设置请… 【iOS Swift Moya 最新请求网络框架封装通用】 前言框架结构1.API定义TargetType2. 配置MoyaProvider3. 网络管理器4. 使用示例注意事项进一步优化 前言
设计一个基于Moya的网络请求框架可以提供灵活的网络请求管理例如设置请求超时时间、暂停某个正在进行的请求等功能。以下让我们一起来设计框架示例包括关键部分的代码实现和详细说明。
框架结构
1. API定义TargetType: 定义所有的API接口。 2. 网络提供者MoyaProvider: 配置MoyaProvider包括自定义Session以设置请求超时时间。 3. 网络管理器、网络响应处理 : 管理请求的发起、暂停和恢复处理网络请求的响应和错误。 4. 使用实例。 1.API定义TargetType 首先定义一个符合TargetType协议的枚举来描述所有的API接口。例如假设我们有一个用户相关的API import Moyaenum UserAPI {case getUser(id: Int)case updateUser(id: Int, parameters: [String: Any])// 其他API接口
}extension UserAPI: TargetType {var baseURL: URL {return URL(string: https://api.example.com)!}var path: String {switch self {case .getUser(let id):return /user/\(id)case .updateUser(let id, _):return /user/\(id)}}var method: Moya.Method {switch self {case .getUser:return .getcase .updateUser:return .put}}var task: Task {switch self {case .getUser:return .requestPlaincase .updateUser(_, let parameters):return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)}}var headers: [String : String]? {return [Content-Type: application/json]}var sampleData: Data {// 提供模拟数据return Data()}
}2. 配置MoyaProvider 为了设置请求超时时间我们需要自定义Session并传递给MoyaProvider。同时为了管理请求任务我们需要保存Cancellable对象。 import Moya
import Alamofireclass NetworkProvider {static let shared NetworkProvider()private let provider: MoyaProviderUserAPIprivate var cancellables: [String: Cancellable] [:]private var pendingRequests: [String: (target: UserAPI, completion: (ResultResponse, MoyaError) - Void)] [:]private let lock NSLock()private init() {let configuration URLSessionConfiguration.defaultconfiguration.timeoutIntervalForRequest 30 // 设置请求超时时间为30秒configuration.timeoutIntervalForResource 60 // 设置资源请求超时时间为60秒let session Alamofire.Session(configuration: configuration)provider MoyaProviderUserAPI(session: session, plugins: [NetworkLoggerPlugin()])}/// 发起网络请求func request(_ target: UserAPI,identifier: String? nil,completion: escaping (ResultResponse, MoyaError) - Void) {let id identifier ?? UUID().uuidStringpendingRequests[id] (target, completion)let cancellable provider.request(target) { [weak self] result inself?.lock.lock()self?.cancellables.removeValue(forKey: id)self?.pendingRequests.removeValue(forKey: id)self?.lock.unlock()completion(result)}lock.lock()cancellables[id] cancellablelock.unlock()}/// 暂停指定的请求func pauseRequest(identifier: String) {lock.lock()if let cancellable cancellables[identifier] {cancellable.cancel()cancellables.removeValue(forKey: identifier)// 请求已取消保留在pendingRequests以便恢复}lock.unlock()}/// 暂停所有请求func pauseAllRequests() {lock.lock()for (id, cancellable) in cancellables {cancellable.cancel()// 请求已取消保留在pendingRequests以便恢复}cancellables.removeAll()lock.unlock()}/// 重新发起指定的请求func resumeRequest(identifier: String) {lock.lock()guard let request pendingRequests[identifier] else {lock.unlock()return}let cancellable provider.request(request.target) { [weak self] result inself?.lock.lock()self?.cancellables.removeValue(forKey: identifier)self?.pendingRequests.removeValue(forKey: identifier)self?.lock.unlock()request.completion(result)}cancellables[identifier] cancellablelock.unlock()}/// 重新发起所有请求func resumeAllRequests() {lock.lock()for (id, request) in pendingRequests {let cancellable provider.request(request.target) { [weak self] result inself?.lock.lock()self?.cancellables.removeValue(forKey: id)self?.pendingRequests.removeValue(forKey: id)self?.lock.unlock()request.completion(result)}cancellables[id] cancellable}lock.unlock()}
}说明 自定义Session: 通过URLSessionConfiguration设置请求超时时间。管理Cancellable: 使用字典cancellables来保存每个请求的Cancellable对象通过唯一标识符管理请求的取消。请求方法: request方法用于发起请求并保存Cancellable对象。暂停请求: pauseRequest方法通过标识符取消指定的请求pauseAllRequests取消所有请求。 3. 网络管理器 创建一个更高层次的网络管理器用于处理不同类型的API请求提供更方便的接口给业务层使用。 import Foundation
import Moyaclass NetworkManager {static let shared NetworkManager()private let networkProvider NetworkProvider.sharedprivate init() {}/// 获取用户信息func getUser(id: Int, identifier: String? nil, completion: escaping (ResultResponse, MoyaError) - Void) {networkProvider.request(.getUser(id: id), identifier: identifier, completion: completion)}/// 更新用户信息func updateUser(id: Int, parameters: [String: Any], identifier: String? nil, completion: escaping (ResultResponse, MoyaError) - Void) {networkProvider.request(.updateUser(id: id, parameters: parameters), identifier: identifier, completion: completion)}/// 暂停指定请求func pauseRequest(identifier: String) {networkProvider.pauseRequest(identifier: identifier)}/// 暂停所有请求func pauseAllRequests() {networkProvider.pauseAllRequests()}/// 重新发起指定请求func resumeRequest(identifier: String) {networkProvider.resumeRequest(identifier: identifier)}/// 重新发起所有请求func resumeAllRequests() {networkProvider.resumeAllRequests()}// 其他API接口的封装
}说明 NetworkManager封装了所有的API接口调用提供统一的入口。 通过identifier参数可以为每个请求指定唯一标识符以便后续管理暂停、取消等。 需要注意的是HTTP请求本身并不支持暂停和恢复只能取消并重新发起请求。如果实现类似暂停和恢复的功能通常需要在请求被取消后重新发起相同的请求。 4. 使用示例 下面是如何使用上述框架进行网络请求、设置超时和暂停请求的示例。 import UIKit
import Moyaclass ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()// 发起获取用户信息的请求let requestIdentifier getUser_123NetworkManager.shared.getUser(id: 123, identifier: requestIdentifier) { result inswitch result {case .success(let response):do {// 解析数据let json try response.mapJSON()print(User Info: \(json))} catch {print(解析错误: \(error))}case .failure(let error):print(请求失败: \(error))}}// 示例在某个时刻暂停请求// NetworkManager.shared.pauseRequest(identifier: requestIdentifier)// 示例在另一个时刻恢复请求// NetworkManager.shared.resumeRequest(identifier: requestIdentifier)}
}说明 发起请求时提供一个唯一的identifier用于后续管理。可以根据业务需求在适当的时机调用pauseRequest或pauseAllRequests来取消请求。
注意事项
暂停和恢复请求: HTTP请求本身不支持暂停和恢复pause操作实际上是取消请求resume操作需要重新发起相同的请求。线程安全: 使用NSLock确保对cancellables和pendingRequests的访问是线程安全的。请求标识符: 为每个请求提供唯一的标识符以便后续管理。可以使用UUID或自定义的字符串。错误处理: 根据实际需求完善错误处理和重试机制。扩展性: 可以根据项目需求进一步扩展网络框架例如添加更多的插件、支持多种认证方式等。
进一步优化
支持多种API接口: 将UserAPI扩展为包含所有需要的API接口或者使用多个TargetType枚举。响应解析: 提供统一的响应解析方法例如使用Codable解析JSON数据。缓存机制: 根据需求添加网络缓存策略提高性能。重试机制: 对于失败的请求添加自动重试机制。 通过以上设计和实现您将拥有一个基于Moya的灵活且功能完整的网络请求框架能够满足设置请求超时时间、暂停和恢复请求等需求。
需要讨论可加VJun_Yeong-Huang