You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift并发捕获变量引用错误:联系人获取代码问题求助

问题解决:enumerateContacts结合async/await的线程安全问题

错误原因

你遇到的Reference to captured var 'contactsArray' in concurrently-executing code错误,本质是线程安全风险:enumerateContacts(with:usingBlock:)的回调block会在任意后台线程执行,而你捕获的contactsArray是普通可变变量,多线程同时读写会引发数据竞争,编译器因此抛出警告。

解决方案

下面提供两种符合Swift并发规范的实现方式,最终都能返回自定义Contact结构体数组:

方案1:用Actor管理线程安全(推荐)

利用Swift的Actor模型自动保证数据访问的线程安全,所有对联系人数组的操作都在Actor上下文内执行:

首先定义自定义Contact结构体:

struct Contact: Identifiable {
    let id: String
    let name: String
    let phoneNumber: String?
}

enum ContactsError: Error {
    case accessDenied
}

实现ContactsActor:

actor ContactsActor {
    private var contacts: [Contact] = []
    
    // 添加联系人(Actor内部保证线程安全)
    private func append(_ contact: Contact) {
        contacts.append(contact)
    }
    
    // 获取完整联系人数组
    private func allContacts() -> [Contact] {
        contacts
    }
    
    // 异步获取联系人的核心方法
    func fetchContacts() async throws -> [Contact] {
        let store = CNContactStore()
        
        // 处理权限
        let authStatus = CNContactStore.authorizationStatus(for: .contacts)
        if authStatus == .notDetermined {
            try await store.requestAccess(for: .contacts)
        }
        guard CNContactStore.authorizationStatus(for: .contacts) == .authorized else {
            throw ContactsError.accessDenied
        }
        
        // 配置联系人请求字段
        let keys = [
            CNContactIdentifierKey,
            CNContactGivenNameKey,
            CNContactFamilyNameKey,
            CNContactPhoneNumbersKey
        ] as [CNKeyDescriptor]
        let request = CNContactFetchRequest(keysToFetch: keys)
        
        return try await withCheckedThrowingContinuation { continuation in
            contacts.removeAll()
            
            do {
                try store.enumerateContacts(with: request) { [weak self] cnContact, stop in
                    guard let self = self else { return }
                    
                    // 转换系统联系人到自定义Contact
                    let fullName = "\(cnContact.givenName) \(cnContact.familyName)".trimmingCharacters(in: .whitespaces)
                    let phone = cnContact.phoneNumbers.first?.value.stringValue
                    let customContact = Contact(id: cnContact.identifier, name: fullName, phoneNumber: phone)
                    
                    // 在Actor上下文添加联系人
                    Task { await self.append(customContact) }
                }
                
                // 枚举完成后,获取完整数组并恢复continuation
                Task {
                    let result = await self.allContacts()
                    continuation.resume(returning: result)
                }
            } catch {
                continuation.resume(throwing: error)
            }
        }
    }
}

在ViewController中调用:

class YourViewController: UIViewController {
    private let contactsActor = ContactsActor()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Task {
            do {
                let contacts = try await contactsActor.fetchContacts()
                // 在这里处理自定义Contact数组
                print("获取到\(contacts.count)个联系人")
            } catch {
                print("获取联系人失败:\(error)")
            }
        }
    }
}

方案2:用串行队列保证线程安全

如果不想用Actor,也可以通过串行队列强制所有数组操作在同一线程执行:

func fetchContacts() async throws -> [Contact] {
    let store = CNContactStore()
    
    // 权限处理同方案1...
    
    let keys = [
        CNContactIdentifierKey,
        CNContactGivenNameKey,
        CNContactFamilyNameKey,
        CNContactPhoneNumbersKey
    ] as [CNKeyDescriptor]
    let request = CNContactFetchRequest(keysToFetch: keys)
    
    return try await withCheckedThrowingContinuation { continuation in
        var contactsArray: [Contact] = []
        let serialQueue = DispatchQueue(label: "com.your.app.contacts.queue")
        
        do {
            try store.enumerateContacts(with: request) { cnContact, stop in
                let fullName = "\(cnContact.givenName) \(cnContact.familyName)".trimmingCharacters(in: .whitespaces)
                let phone = cnContact.phoneNumbers.first?.value.stringValue
                let contact = Contact(id: cnContact.identifier, name: fullName, phoneNumber: phone)
                
                // 用串行队列同步修改数组,避免线程竞争
                serialQueue.sync {
                    contactsArray.append(contact)
                }
            }
            
            // 枚举完成后,在串行队列内返回数组
            serialQueue.sync {
                continuation.resume(returning: contactsArray)
            }
        } catch {
            continuation.resume(throwing: error)
        }
    }
}

核心逻辑说明

两种方案的本质都是确保联系人数组的读写操作在同一串行上下文执行:

  • Actor通过Swift并发运行时自动管理执行上下文,避免数据竞争;
  • 串行队列通过GCD强制所有数组操作按顺序执行,保证线程安全。

内容的提问来源于stack exchange,提问作者Sh-RA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:50:07