结合开发简单介绍一下vue_结合介绍

结合开发简单介绍一下vue

Start your journey inside Apple framework dedicated to asynchronous programming

在致力于异步编程的Apple框架内开始您的旅程

介绍 (Introduction)

Before going deep inside the Combine Framework, it’s better to recall the difference between synchronous and asynchronous programming, in order to outline the context in which we are going to work.

在深入Combine框架之前 ,最好回顾一下同步编程和异步编程之间的区别,以便概述我们将要工作的上下文。

With synchronous programming, we’re referring to all that programs wrote in a single-threaded language that execute sequentially line-by-line. Because of that, we’re always sure about the current state of our variables.

对于同步编程,我们指的是以单线程语言编写的所有程序,它们逐行顺序执行。 因此,我们始终可以确定变量的当前状态。

With asynchronous programming, instead, we’re referring to all that programs wrote in a multi-threaded language that are running on an asynchronous event-driven UI framework like UIKit. In these situation, because more than one thread are executed at the same time, it’s difficult to understand what is causing some errors in the code.

相反,在异步编程中,我们指的是在异步事件驱动的UI框架(如UIKit)上运行的所有使用多线程语言编写的程序。 在这种情况下,由于同时执行多个线程,因此很难理解导致代码出错的原因。

Precisely this type of difficulty led to the introduction of a framework like Combine. From Apple documentation, we know that: “The Combine framework provides a declarative approach for how your app processes events. Rather than potentially implementing multiple delegate callbacks or completion handler closures, you can create a single processing chain for a given event source. Each part of the chain is a Combine operator that performs a distinct action on the elements received from the previous step. “. Obviously this definition, however accurate it is, sound a little too abstract and might and might cause perplexity to those who have never had to deal with Combine.

正是这种困难导致引入像Combine这样的框架。 从苹果公司的文件,我们知道:“ 有机结合起来框架 提供了如何您的应用程序进程的事件的声明的做法。 您可以为给定事件源创建单个处理链,而不必潜在地实现多个委托回调或完成处理程序闭包。 链的每个部分都是一个Combine运算符,它对上一步收到的元素执行不同的操作。 ”。 显然,这个定义无论多么精确,听起来都太抽象了,可能使那些从未需要处理Combine的人感到困惑。

But the goal of this tutorial is exactly to make easier the first approach with this framework, so we’re now going to see Combine’s basic elements.

但是,本教程的目标是使该框架的第一种方法更加容易,因此,我们现在将了解Combine的基本元素。

为什么要使用合并? (Why use Combine?)

Using Combine is not necessary to make a good app, because everything we’ve seen can be built in a custom way.

使用Combine并不是制作一个好的应用程序的必要,因为我们所看到的一切都可以以自定义方式构建。

But its usage allows you to add another level of abstraction, safe and efficient, to your asynchronous code. This means a better integration that’s well tested and supported.

但是,它的用法使您可以向异步代码添加另一种安全有效的抽象级别 。 这意味着一个经过良好测试和支持的更好的集成。

In fact, usually, asynchronous code testing is very complex, while with Combine you’re going to use operators already tested, so that you can only worry about the implementation logic of the app. To these benefits, there is also the fact that Combine isn’t a framework that affects how you structure your app because it doesn’t alter how you would separate responsibilities in your project (For example, Model-View-Controller) and it’s also possible to use it only in some parts of the code.

实际上,通常,异步代码测试非常复杂,而使用Combine时,您将使用已经测试过的运算符,因此您只需要担心应用程序的实现逻辑。 对于这些好处,还有一个事实,那就是Combine不会影响您构建应用程序的框架,因为它不会改变您将项目中的职责分开的方式(例如,Model-View-Controller),而且可能仅在代码的某些部分中使用它。

基本要素 (Basic Elements)

The three key moving pieces in Combine are:

合并中的三个关键移动部分是:

  1. Publishers

    出版商
  2. Operators

    经营者
  3. Subscribers

    订户

We’re now going to have an introduction about all of them, to then focus in this tutorial about Publishers, Subscribers and their protocols.

现在,我们将对所有这些内容进行介绍,然后在本教程中重点介绍发布者,订阅者及其协议。

出版商 (Publishers)

Publishers are type that can emit values over time to one or more interested parties, like the subscribers. Every publisher can emit multiple events of these three types:

发布者是一种可以随时间向一个或多个感兴趣的方(例如订阅者)发出值的类型。 每个发布者都可以发出以下三种类型的多个事件

  1. An output value of the publisher’s generic Output type

    发布者的通用输出类型的输出值

  2. A successful completion

    成功完成

  3. A completion with an error of the publisher’s Failure type

    具有发布者的失败类型错误的完成

A publisher can emit zero or more output values and, if it ever completes (Either successfully or due to a failure), it will not emit any other events. Thanks to these three types of events emitted, we can assign to the publisher any kind of task inside the application, from reacting to user gestures to making network calls. One of the publisher’s best feature is that they come with error handling built-in. The Publisher protocol is generic over two types:

发布者可以发出零个或多个输出值,并且一旦完成(无论成功还是由于失败),它就不会发出任何其他事件。 由于发出了这三种类型的事件,我们可以为发布者分配应用程序内的任何类型的任务,从响应用户手势到进行网络调用。 发布者最好的功能之一是内置了错误处理功能。 Publisher协议在以下两种类型上是通用的:

  1. Publisher .Output: Type of publisher’s output value

    Publisher .Output:发布者的输出值的类型

  2. Publisher .Failure: Type of the error that the publisher can throw if it fails

    Publisher .Failure:如果发布者失败,则引发的错误的类型

When you subscribe to a given publisher, you know what values to expect from it and which errors it could fail with.

订阅给定的发布者时,您知道可以从中获得什么价值以及可能会失败的错误。

经营者 (Operators)

Operators are methods declared on the Publisher protocol that return either the same or a new publisher. That is very useful because we can call a bunch of operators one after the other, effectively chaining them together.

运算符是在发布者协议上声明的方法,它们返回相同或新的发布者。 这非常有用,因为我们可以一个接一个地调用一堆运算符,将它们有效地链接在一起。

These operators can be combined together to implement very complex logic over the execution of a single subscription. It’s impossible to put them in the wrong order because one’s output will not match the next one’s input.

这些运算符可以组合在一起,以在单个订阅的执行上实现非常复杂的逻辑。 不可能以错误的顺序排列它们,因为一个人的输出将与下一个人的输入不匹配。

They always have an input and an output, commonly referred to as upstream and downstream, in order to avoid shared state.

它们始终具有输入和输出,通常称为上游和下游,以避免共享状态。

Operators focus on working with the data they receive from the previous operator and on provide their output to the next one. This means that no other asynchronously-running piece of code can modify the data they’re working on in the meanwhile.

操作员专注于处理从上一个操作员那里接收到的数据,并将其输出提供给下一个操作员。 这意味着没有其他异步运行的代码段可以同时修改他们正在处理的数据。

订户 (Subscribers)

At the end of the subscription chain, we found the subscribers. They usually do something with the output or the completion event emitted by the chain. Actually Combine provide two built-in subscribers:

在订阅链的末尾,我们找到了订户 。 他们通常对链发出的输出或完成事件进行处理。 实际上,合并提供了两个内置的订户:

  • Sink: This type of subscriber allows you to provide closures with your code that will receive output values and completions, which it could then work on.

    接收器:这种类型的订阅服务器允许您为代码提供闭包,这些闭包将接收输出值和补全,然后可以对其进行处理。

  • Assign: Allows you to, without the need of custom code, bind the resulting output to some property on your data model or an a UI control.

    分配:使您无需自定义代码即可将结果输出绑定到数据模型或UI控件的某些属性。

In case these are not enough, it’s possible to override Combine protocol to create a custom one.

如果这些还不够,可以覆盖Combine协议以创建自定义协议。

订阅内容 (Subscriptions)

When you add a subscriber at the end of a subscription, it activates the publisher at the beginning of the chain. This concept is really important, because publishers don’t emit any value if there are no subscribers to potentially receive the output. The subscriptions allow you to declare a chain of asynchronous event with their own custom code and error handling only once.

当您在预订的末尾添加一个用户,它会激活在链开始发行。 这个概念非常重要,因为如果没有订阅者潜在地接收输出,则发布者不会产生任何价值。 订阅允许您使用自己的自定义代码和错误处理声明一次异步事件链。

Moreover, you don’t need to specifically memory manage a subscription, thanks to a protocol provided by Combine called Cancellable. Once the whole chain, that conform to this protocol, returns a Cancellable object and this is then released from the memory, so the whole subscription releases its resources from the memory. This means that you can bind the lifespan of a subscription by storing it in a property on your view controller. Every time that the user dismisses the view controller from the stack, the subscription will be canceled.

此外,由于Combine所提供的称为Cancellable的协议,您无需专门进行内存管理预订。 一旦符合此协议的整个链返回一个Cancelableable对象,然后将其从内存中释放出来,因此整个订阅将从内存中释放其资源。 这意味着您可以通过将订阅的生命周期存储在视图控制器的属性中来绑定它。 每次用户从堆栈中退出视图控制器时,订阅都将被取消。

发布者和订阅者 (Publishers & Subscribers)

We’re now going to see more in detail two of the three key moving pieces of Combine, starting with their protocol, then we’ll see some examples of already built-in classes.

现在,从协议开始,我们将更详细地看到Combine的三个关键动作中的两个,然后我们将看到一些已经内置的类的示例。

发布者协议 (Publisher Protocol)

Image for post

As you can see from the code, the two associatedtype are the publisher’s interface that a subscriber must match in order to create a subscription. Now we’ll see some publisher examples:

从代码中可以看到,两个关联类型是订阅者必须匹配才能创建订阅的发布者接口。 现在,我们将看到一些发布者示例:

  • .publisher

    发布者

As first example, we’ll use the NotificationCenter, that now has a method named publisher(for:object:), that provides a Publisher type that can publish broadcasted notifications.

作为第一个示例,我们将使用NotificationCenter ,它现在具有一个名为Publisher(for:object :)的方法 ,该方法提供可以发布广播的通知的Publisher类型。

Image for post

This method has been added in order to make NotificationCenter’s API compatible with Combine.

已添加此方法,以使NotificationCenter的API与Combine相兼容。

  • Just

    只是

A publisher created with Just emits its output value to the subscribers only once and then completes. That publisher will be of primitive value type.

Just创建的发布者仅将其输出值发送给订阅者一次,然后完成。 该发布者将具有原始值类型。

Image for post
  • Future

    未来

A future can be used to create a publisher that will produce asynchronously a single value and then will complete. This happens by invoking a closure when a value or an error is made available, and that closure is referred to as a promise.

可以使用future创建发布者,该发布者将异步生成单个值,然后将其完成。 这是通过在值或错误可用时调用闭包来实现的,并且将该闭包称为promise。

Unlike other publishers, a future execute as soon as it’s created, without requiring a subscriber.

与其他发布者不同,将来的发布会在创建后立即执行,而无需订阅者。

Image for post

订户和订阅协议 (Subscribers & Subscription Protocol)

Image for post

As we’ve already seen, the subscribers are what allow you to activate the entire subscription chain. In this case, the two associatedtype must match with those of the publisher to whom you want to subscribe. The subscription is the connection between publisher and subscriber and its protocol is:

正如我们已经看到的, 订阅者可以使您激活整个订阅链 。 在这种情况下,两个关联的类型必须与您要订阅的发布者的类型匹配。 订阅是发布者和订阅者之间的连接,其协议为:

Image for post

The subscriber calls request(_:) to indicate it is willing to receive more values, up to a max number or unlimited.

订户调用request(_ :)表示它愿意接收更多的值,最大为最大值或无限制。

定制订户 (Custom Subscriber)

Now that we’ve seen the subscriber protocol in detail, it’s time to make them on our own, using an example:

现在,我们已经详细了解了订户协议,现在是使用示例自己制作订户协议的时候了:

Image for post

In this case, we’re creating a subscriber that accepts Integer as Input and we’re specifying that it will never receive errors. After that, the three required methods are implemented:

在这种情况下,我们正在创建一个接受Integer作为Input的订户,并且指定它永远不会收到errors 。 之后,实现了三种必需的方法:

  • The first one is the one called by the publisher, which calls the request method on the subscription specifying that the subscriber is willing to receive up to three values upon subscription.

    一个是由发布者调用的,它在订阅上调用request方法,以指定订阅者愿意在订阅时接收多达三个值。

  • The second one works on the received values. In this case, it only prints them

    第二个处理接收到的值。 在这种情况下,仅打印它们

  • The third one works on the received completion event. In this case, it only prints it.

    第三个处理接收到的完成事件。 在这种情况下,仅打印它。

To subscribe this subscriber to the publisher, you only have to add:

要将订阅者订阅到发布者,只需添加:

Image for post

After we’ve seen how to create a custom subscriber, we’re now going to see some examples about the already built-in subscriber:

在了解了如何创建自定义订阅服务器之后,我们现在将看到一些有关已内置订阅服务器的示例:

  • Sink(_:_:)

    水槽(_:_:)

To create a Subscriber we can call the sink operator, that provide a closure to manage the publisher’s output, that can be a value or a completion event. With sink, the subscriber will continue to receive values as the publisher produces them. This is called unlimited demand.

要创建订户,我们可以调用接收运算符,该运算符提供一个闭包来管理发布者的输出,可以是值或完成事件。 使用接收 ,订阅者将在发布者生成它们时继续接收值。 这称为无限需求

Image for post
  • Assign(to:on:)

    分配(至:on :)

It is possible to create a subscriber also using the Assign operator. This operator enables you to assign the received value to a KVO-compliant (Key-Value Observing programming) property of an object.

也可以使用Assign运算符创建订户。 此运算符使您可以将接收到的值分配给对象的KVO兼容(键值观察编程)属性。

Image for post

如何取消订阅 (How to cancel a subscription)

When a subscriber is done and no longer wants to receive values from a publisher, it’s a good idea to cancel the subscription to free up resources. This is made possible because the subscription return an instance of AnyCancellable that, conforming to the Cancellable protocol, provides to the subscription the cancel() method.

订阅者完成操作并且不再希望从发布者那里获得价值时,最好取消订阅以释放资源。 之所以可以这样做,是因为订阅返回了AnyCancellable的实例,该实例符合Cancellable协议 ,向订阅提供了cancel()方法。

Image for post

学科 (Subject)

We’re going to see a particular type of publisher, the subjects, which are very important because they act as a “bridge” to enable non-Combine code to send values to Combine subscribers. This is possible because they allow you to send values “On-demand”. There are two types of subject:

我们将看到一种特定类型的发布者, 主题 ,这是非常重要的,因为它们充当了“桥梁”,使非Combine代码可以将值发送给Combine订阅者。 这是可能的,因为它们允许您“按需”发送值。 有两种类型的主题

  • Passthrough Subject

    直通主题

Image for post

In this case we’ve created a Passthrough subject that allows you to send Integer. As we can see from the code, it’s possible to emit either values of the specified type or completion event.

在这种情况下,我们创建了一个传递主题 ,可以让您发送整数。 从代码中可以看到,可以发出指定类型的值或完成事件。

  • CurrentValue Subject

    CurrentValue主题

The CurrentValue Subject is as similar as the previous one, but it also allows you to access to its current value at any time using “subject.value”. Its value is equal to the last value sent.

CurrentValue主题与上一个主题相似,但是它还允许您随时使用“ subject.value”访问其当前值。 它的值等于最后发送的值。

Image for post

Congratulation, you’ve just found out the basics of this new and exciting framework. To see all the code used in this article, or if you want to see other Combine examples, visit our repository on GitHub:

恭喜 ,您已经找到了这个令人兴奋的新框架的基础。 要查看本文中使用的所有代码,或者如果您想查看其他Combine示例,请访问我们在GitHub上的存储库:

翻译自: https://medium.com/swlh/an-introduction-to-combine-9d9a62afa34d

结合开发简单介绍一下vue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值