02 · Redux 是什么,又不是什么

英文原文:Part 2 --- What Redux Is (and Isn't)

**你在哪里:**第二阶段,定义与边界。

**读完本篇你会知道:**一句话定义,三大原则,Redux 不管哪些事,以及它和 MVC、MVVM、ReSwift、TCA 的关系。

缩写对照表

缩写 英文全称 中文
UDF Unidirectional Data Flow 单向数据流
MVC Model-View-Controller 模型-视图-控制器
MVVM Model-View-ViewModel 模型-视图-视图模型
TCA The Composable Architecture 可组合架构
VC View Controller 视图控制器
UI User Interface 用户界面

2.1 一句话定义

Redux 是这样一种模式:整个应用的状态是一个值,由一个 store 持有;修改它的唯一办法是派发一个 action,再由一个纯函数 reducer 把它变成下一个状态。

它是一种模式 ,不是框架。ShopList 用到的整个库大约只有 120 行代码(5.4,英文)。

2.2 三大原则

原则 含义 在 Swift 里
单一数据源 应用的所有状态都在同一个 store 的同一棵树里 struct AppState,一个 Store<AppState, AppAction> 实例
状态只读 不能给状态赋值,只能用 action 描述一次变化 public private(set) var state,唯一的入口是 dispatch(_:)
用纯函数修改 (State, Action) -> State,没有副作用,相同输入总得到相同输出 func appReducer(_ state: AppState, _ action: AppAction) -> AppState

Swift 对这三条都有帮助:struct 带来值语义(各个副本互不影响),带关联值的 enum 能精确描述 action,private(set) 则强制状态只读。

2.3 单向数据流

数据往哪个方向走?(这就是 UDF,Unidirectional Data Flow,单向数据流。)
#mermaid-svg-VoM7j92gjT3Sb1Fh{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-VoM7j92gjT3Sb1Fh .error-icon{fill:#552222;}#mermaid-svg-VoM7j92gjT3Sb1Fh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VoM7j92gjT3Sb1Fh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .marker.cross{stroke:#333333;}#mermaid-svg-VoM7j92gjT3Sb1Fh svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VoM7j92gjT3Sb1Fh p{margin:0;}#mermaid-svg-VoM7j92gjT3Sb1Fh .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .cluster-label text{fill:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .cluster-label span{color:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .cluster-label span p{background-color:transparent;}#mermaid-svg-VoM7j92gjT3Sb1Fh .label text,#mermaid-svg-VoM7j92gjT3Sb1Fh span{fill:#333;color:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .node rect,#mermaid-svg-VoM7j92gjT3Sb1Fh .node circle,#mermaid-svg-VoM7j92gjT3Sb1Fh .node ellipse,#mermaid-svg-VoM7j92gjT3Sb1Fh .node polygon,#mermaid-svg-VoM7j92gjT3Sb1Fh .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .rough-node .label text,#mermaid-svg-VoM7j92gjT3Sb1Fh .node .label text,#mermaid-svg-VoM7j92gjT3Sb1Fh .image-shape .label,#mermaid-svg-VoM7j92gjT3Sb1Fh .icon-shape .label{text-anchor:middle;}#mermaid-svg-VoM7j92gjT3Sb1Fh .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .rough-node .label,#mermaid-svg-VoM7j92gjT3Sb1Fh .node .label,#mermaid-svg-VoM7j92gjT3Sb1Fh .image-shape .label,#mermaid-svg-VoM7j92gjT3Sb1Fh .icon-shape .label{text-align:center;}#mermaid-svg-VoM7j92gjT3Sb1Fh .node.clickable{cursor:pointer;}#mermaid-svg-VoM7j92gjT3Sb1Fh .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .arrowheadPath{fill:#333333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VoM7j92gjT3Sb1Fh .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-VoM7j92gjT3Sb1Fh .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VoM7j92gjT3Sb1Fh .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-VoM7j92gjT3Sb1Fh .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .cluster text{fill:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh .cluster span{color:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VoM7j92gjT3Sb1Fh .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-VoM7j92gjT3Sb1Fh rect.text{fill:none;stroke-width:0;}#mermaid-svg-VoM7j92gjT3Sb1Fh .icon-shape,#mermaid-svg-VoM7j92gjT3Sb1Fh .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VoM7j92gjT3Sb1Fh .icon-shape p,#mermaid-svg-VoM7j92gjT3Sb1Fh .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-VoM7j92gjT3Sb1Fh .icon-shape .label rect,#mermaid-svg-VoM7j92gjT3Sb1Fh .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VoM7j92gjT3Sb1Fh .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-VoM7j92gjT3Sb1Fh .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-VoM7j92gjT3Sb1Fh :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Action
dispatch
Middleware
Reducer
状态
视图

图里没有从视图指回状态的箭头。视图不能写 state.items.append(...);它只能发出一个 action,然后等新状态传回来。正是这个单向的环,让每一次变化都有迹可循。

2.4 边界:Redux 不是什么

Redux 不是...... 因为......
UI 框架 它什么都不画,渲染仍由 UIKit 或 SwiftUI 负责
网络层或持久化层 它只给副作用一个安身之处(middleware);URLSession 和文件读写的代码还得你自己写
视图局部状态的替代品 文本框的光标位置、滚动偏移量通常留在视图里(见 7.3,英文)
导航系统 你可以把导航放进状态,但 Redux 本身没有规定怎么做
免费的 每个事件都要变成一个 action 和一个 reducer 分支:小应用的代码会变多

2.5 和你熟悉的架构对比

MVC MVVM Redux
数据归谁? 每个视图控制器(或它持有的模型) 每个 view model 同一个 store
数据怎么变? 到处都能直接修改 view model 的方法 dispatch(action) → reducer
视图怎么更新? 手动刷新、delegate、通知 绑定(Combine、闭包、@Published) 订阅 store,按整个状态渲染
副作用 在视图控制器里 在 view model 或服务里 Middleware
跨页面同步 你自己解决 你自己解决 自动:状态是同一份
测试单元 难(需要 UI) view model **Reducer(纯函数)**和 middleware

Redux 和 MVVM 可以结合:view model 订阅 store,把 AppState 变成可以直接显示的字符串。Commencis 的文章展示了这种风格。本系列让视图直接连接 store,好让 Redux 的各个部分一目了然。

2.6 自己写,还是用库

选择 你得到什么 适合什么情况
自己写 store(本系列) 约 120 行你完全看得懂的代码;没有依赖 学习;中小型应用;想自己掌控的团队
ReSwift 成熟的 Redux 移植:store、middleware、支持 select 和 skipRepeats 的订阅 想用经典 Redux,又不想自己写
TCA 类似 Redux 的核心,外加副作用、依赖注入、导航和测试工具,围绕 SwiftUI 构建 大型 SwiftUI 应用;愿意采用一个框架

自己动手写 store 学到的一切,换到这两个库上同样适用。


← 01 · 为什么要在 iOS 上用 Redux · 下一篇 → 03 · 概念地图

相关推荐
楚识科技42 分钟前
移动端 OCR SDK 集成拆解:Android/iOS 离线识别与结构化回填
android·ios·ocr
CocoaKier15 小时前
神技分享:App加锁,App隐藏?苹果官方自带,只需要一个长按
ios·apple
传奇开心果编程1 天前
【六大移动UI框架比较学与练】第5课:交互与导航——事件处理、手势与页面路由实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
Geek-Chow1 天前
01 · 为什么要在 iOS 上用 Redux
ios
传奇开心果编程2 天前
【六大移动UI框架比较学与练】第3课:文本、图片、布局核心组件实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
艾欧饲2 天前
能力、Entitlement 与扩展:侧载的 App 为什么装上了,有的功能却不能用
ios
庚鑫2 天前
只改最低支持版本,为什么 iOS 播放器在初始化时崩了?
ios
黑科技iOS上架2 天前
机审友好的iOS混淆方案:无固定二进制特征才能过审4.3
flutter·ios·objective-c·cocoa·审核
传奇开心果编程2 天前
【六大移动UI框架比较学与练】第4课:列表与滚动——懒加载、复用与状态保持实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack