# 共享附加作用

文档中一些特征序列，会有如下特性：

共享 [附加作用](https://3440217568.gitbook.io/rxswift/recipes/side_effects)：

* [Driver](https://3440217568.gitbook.io/rxswift/rxswift_core/observable/driver)
* [Signal](https://3440217568.gitbook.io/rxswift/rxswift_core/observable/signal)
* [ControlEvent](https://3440217568.gitbook.io/rxswift/rxswift_core/observable/control_event)
* ...

不共享 [附加作用](https://3440217568.gitbook.io/rxswift/recipes/side_effects)：

* [Single](https://3440217568.gitbook.io/rxswift/rxswift_core/observable/single)
* [Completable](https://3440217568.gitbook.io/rxswift/rxswift_core/observable/completable)
* [Maybe](https://3440217568.gitbook.io/rxswift/rxswift_core/observable/maybe)
* ...

那什么是**共享** [**附加作用**](https://3440217568.gitbook.io/rxswift/recipes/side_effects)，什么是**不共享** [**附加作用**](https://3440217568.gitbook.io/rxswift/recipes/side_effects)？

## 共享 [附加作用](https://3440217568.gitbook.io/rxswift/recipes/side_effects)：

```swift
...
let observable: Observable<Teacher> = API.teacher(teacherId: 1)
let shareSideEffects: Driver<Teacher> = observable.asDriver(onErrorDriveWith: .empty())

let observer0: (Teacher) -> () = ...
let observer1: (Teacher) -> () = ...

shareSideEffects.drive(onNext: observer0)
shareSideEffects.drive(onNext: observer1) // 第二次订阅
```

如果一个序列**共享** [**附加作用**](https://3440217568.gitbook.io/rxswift/recipes/side_effects)，那在第二次订阅时，不会重新发起网络请求，而是共享第一次网络请求（[附加作用](https://3440217568.gitbook.io/rxswift/recipes/side_effects)）。

## 不共享 [附加作用](https://3440217568.gitbook.io/rxswift/recipes/side_effects)：

```swift
...
let observable: Observable<Teacher> = API.teacher(teacherId: 1)
let notShareSideEffects: Single<Teacher> = observable.asSingle()

let observer0: (Teacher) -> () = ...
let observer1: (Teacher) -> () = ...

notShareSideEffects.subscribe(onSuccess: observer0)
notShareSideEffects.subscribe(onSuccess: observer1) // 第二次订阅
```

如果一个序列**不共享** [**附加作用**](https://3440217568.gitbook.io/rxswift/recipes/side_effects)，那在第二次订阅时，会重新发起网络请求，而不是共享第一次网络请求（[附加作用](https://3440217568.gitbook.io/rxswift/recipes/side_effects)）。

因此我们需要注意，如果一个网络请求序列，他**不共享** [**附加作用**](https://3440217568.gitbook.io/rxswift/recipes/side_effects)，那每一次订阅时就会单独发起网络请求。这时最好改用 **共享** [**附加作用**](https://3440217568.gitbook.io/rxswift/recipes/side_effects) 的序列，或者使用 [share](https://3440217568.gitbook.io/rxswift/decision_tree/sharereplay) 操作符。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://3440217568.gitbook.io/rxswift/recipes/share_side_effects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
