> ## Documentation Index
> Fetch the complete documentation index at: https://docs.y.uno/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Test in the sandbox first: base URL https://api-sandbox.y.uno (Test Mode in the Dashboard). Production is https://api.y.uno (Live Mode).
> Every API request needs the public-api-key and private-secret-key headers. Get them in the Dashboard at https://dashboard.y.uno/developers (Developers > Credentials); sandbox and production keys are different.
> Send an X-Idempotency-Key (a UUID) when creating payments, refunds and cancels, so retries do not create duplicates.
> Start with the Quickstart (https://docs.y.uno/docs/quickstart.md) and the agent guide (https://docs.y.uno/agents.md). To search these docs from a coding agent, connect the docs MCP server at https://docs.y.uno/mcp.

# Swift 6 并发

> 在实现 Yuno 的支付和注册 delegate 协议时，处理 Swift 6 的并发要求

> [!NOTE]
>
> **This page documents a legacy SDK.** Starting a new integration? Use the [Yuno SDK](https://docs.y.uno/docs/yuno-sdk/get-started.md). Already on a legacy SDK? Follow the [migration guide](https://docs.y.uno/docs/yuno-sdk/migration/overview.md).
>
> The new **Yuno SDK** (Web v2.x, iOS v3.x, Android v3.x, React Native v2.x) replaces the Full, Lite, Seamless, Render, Headless, and Secure Fields SDKs with one API across every platform. Legacy SDKs remain supported side by side during the transition and are scheduled for deprecation in **2027**. Contact your Account Manager for migration planning.

Swift 6 引入了更严格的并发要求，这会影响您实现 SDK delegate 协议（例如 `YunoPaymentDelegate`、`YunoEnrollmentDelegate`）的方式。本节说明其中的挑战，并针对不同的实现场景提供解决方案。

> [!NOTE]
>
> **了解 Swift 6 中的并发**
>
> 并发是指您的应用同时管理多个任务的能力。在 Swift 6 中，并发规则变得更加严格，以增强应用稳定性并防止崩溃。

## 问题

在 Swift 6 中，继承自 `Sendable` 的协议要求其所有实现都是线程安全的。在标记为 `@MainActor` 的类中实现 delegate 时，这会产生警告。

线程安全是指您的代码可以从多个线程安全地调用，而不会导致崩溃或意外行为。`@MainActor` 确保代码在主线程（UI 线程）上运行。

## 设计决策

SDK 不将协议标记为 `@MainActor`，原因如下：

* 这会强制所有实现都必须兼容 `MainActor`
* 这会降低不使用 `MainActor` 的商户的灵活性
* 每种实现都有不同的并发需求

## 商户的责任

商户有责任根据自己的实现来处理并发。以下是三种不同的方法，您可以根据具体需求选用。

### 方案 1：不可变属性

此方法使用自动具备线程安全性的不可变属性，非常适合配置值固定、在运行时不会改变的简单应用。

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
@MainActor
class MyViewController: UIViewController, YunoPaymentDelegate {

    private let _countryCode = "CO"
    private let _language = "EN"
    private let _checkoutSession = "session_id"

    nonisolated var countryCode: String { _countryCode }
    nonisolated var language: String? { _language }
    nonisolated var checkoutSession: String { _checkoutSession }

    nonisolated func yunoPaymentResult(_ result: Yuno.Result) {
        Task { @MainActor in
            // Handle result on main thread
        }
    }
}
```

### 方案 2：使用 `MainActor.assumeIsolated` 的可变属性

此方法通过使用 `MainActor.assumeIsolated`，在保持线程安全的同时允许使用可变属性。它最适合配置值可能在运行时改变的应用，例如用户偏好设置。

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
@MainActor
class MyViewController: UIViewController, YunoPaymentDelegate {

    @Published var configLanguage: String = "EN"
    @Published var configCountryCode: String = "CO"

    nonisolated var language: String? {
        MainActor.assumeIsolated { configLanguage }
    }

    nonisolated var countryCode: String {
        MainActor.assumeIsolated { configCountryCode }
    }
}
```

### 方案 3：适用于非 `MainActor` 类

此方法适用于不需要 `MainActor` 隔离的服务类。它最适合不与 UI 交互的后台服务或工具类。

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
class MyService: YunoPaymentDelegate {

    let countryCode: String
    let language: String?
    let checkoutSession: String
    let viewController: UIViewController?

    init(countryCode: String, language: String?, checkoutSession: String, viewController: UIViewController?) {
        self.countryCode = countryCode
        self.language = language
        self.checkoutSession = checkoutSession
        self.viewController = viewController
    }

    func yunoPaymentResult(_ result: Yuno.Result) {
        // Handle result
    }
}
```

> [!NOTE]
>
> 前面的示例使用的是 `YunoPaymentDelegate`，但同样的原则也适用于 `YunoEnrollmentDelegate`。实现注册 delegate 时，应使用 `customerSession` 而不是 `checkoutSession`。

## 重要注意事项

在 delegate 中实现并发时，请牢记以下要点：

* `MainActor.assumeIsolated`：仅在您能保证其从 `MainActor` 调用时使用。这是一种安全机制，用于告诉 Swift“相信我，我知道这段代码运行在主线程上”。
* `nonisolated`：表示它可以从任何线程访问，因此必须是线程安全的。当您的属性或方法不依赖 UI 状态时使用它。
* `viewController`：delegate 协议将此属性声明为 `@MainActor`，因为它应始终从主线程访问。UI 组件必须始终在主线程上运行，以防止崩溃。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.