基于 Moya 和 PromiseKit 的网络请求层 SGAPIRequest

码农软件 · 软件分类 · 网络工具包 · 2019-02-24 19:28:46

软件介绍

SGAPIRequest 是用 Moya + PromiseKit 创建具有序列化及缓存功能的网络请求层。

第一次使用Swift来做项目,第一步当然就是折腾架构,由于项目时间比较宽裕,使用了较多的时间来进行学习参照,最后使用了这样的架构来作为这个产品的网络层,满足了项目基本的需求,肯定有不少的缺陷,权当各位参考。

先来看看最终我们要达成的目标,下面是一段异步请求接口的代码,这段代码具有网络请求、序列化及基本的缓存功能

firstly { () -> Promise<[ArticleCategoryModel?]?> in
    return CallApi(DFAPI.navigations, isCached: true)
}.then { [weak self] (data) -> Void in
    self!.navigations = data!
}

这一段请求,由以下4个库实现:

  1. Moya:负责网络请求

  2. PromiseKit:负责干净优雅的链式异步调用

  3. HandyJSON:负责序列化、反序列化

  4. AwesomeCache:负责缓存请求结果及缓存过期

项目Demo

本文地址:https://codercto.com/soft/d/54.html

Programming From The Ground Up

Programming From The Ground Up

Jonathan Bartlett / Bartlett Publishing / 2004-07-31 / USD 34.95

Programming from the Ground Up is an introduction to programming using assembly language on the Linux platform for x86 machines. It is a great book for novices who are just learning to program as wel......一起来看看 《Programming From The Ground Up》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具