16

深入浅出 Apple 响应式框架 Combine

 3 years ago
source link: https://www.infoq.cn/article/eAQ01u5JEVuvQfghlqBS
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一、引言

Combine.framework 是 Apple 在 2019 WWDC 上基于 Swift 推出的函数响应框架(Functional Reactive Programming),支持 Apple 全平台的操作系统(iOS13+,macOS 10.15+等)。函数式响应框架无论在哪个平台早已流行泛滥,开源的 Rx 更是实现了各种语言的响应式编程框架。Apple 在这个时候推出响应式框架,无疑是对自己护城河的进一步巩固。事实上 SwiftUI 的数据驱动就是依赖 Combine。

本文将深入浅出地介绍 Combine 的基本概念和原理,然后通过具体 demo 详细阐述其在实际编码中的应用。

二、什么是 Combine

Combine is Swift declarative framework for processing values over time

Combine 为处理随着时间变化的值的一种声明式框架。Combine 作用是将异步事件通过组合事件处理操作符进行自定义处理。关注如何处理变化的值,正是响应式编程的核心。

Combine 可以概述为一种声明式的函数响应式编程,简洁用下图表示:

IfAB7vu.png!mobile

通过此图,我们可以总结 Combine 是什么:

Combine = Publishers + Operators + Subscribers

2.1 Publishers

Publisher sends sequences of values over time to one or more Subscribers.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK