向用户显示iCloud同步状态

Display iCloud sync status to user

提问人:Petr Smejkal 提问时间:11/3/2023 最后编辑:jnpdxPetr Smejkal 更新时间:11/3/2023 访问量:58

问:

我正在将 Core Data 和 iCloud 同步设置与 NSPersistentCloudKitContainer 一起使用。当用户首次在第二台设备上打开应用程序时,iCloud 需要一些时间才能从云中获取所有数据。我想向用户显示 iCloud 同步正在进行中 ProgressView()。

我尝试了以下代码:

import Combine
import CoreData
import SwiftUI

class CloudSyncManager: ObservableObject {
    @Published var isSyncing: Bool = false

    private var cancellables: Set<AnyCancellable> = []

    init() {
        NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification)
            .sink { [weak self] notification in
                guard let self else { return }

                if let event = notification.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey]
                    as? NSPersistentCloudKitContainer.Event
                {
                    let sync = event.type == .import || event.type == .export || event.type == .setup
                    
                    DispatchQueue.main.async {
                        self.isSyncing = sync
                        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                            self.isSyncing = false
                        }
                    }
                }
            }
            .store(in: &cancellables)
    }
}

使用此代码时,将设置为同步过程的最后阶段(最后一秒或两秒),而控制台显示工作正在完成更长的时间。有什么方法可以“更快”通知用户吗?isSyncingtrue

iOS SwiftUI Core-Data iCloud

评论

0赞 workingdog support Ukraine 11/3/2023
Is there any way to notify the user "sooner"?,早于何时?比设置iCloud同步时更快?或者您是关于如何显示 ProgressView() 的问题?
0赞 Petr Smejkal 11/3/2023
较早 = iCloud 开始设置时。iCloud 在应用程序启动期间已启动设置,并将日志打印到控制台中。这大约需要 30-40 秒。在此期间,导入、导出或设置事件类型均没有来自 NotificationCenter 的事件。仅在同步结束时收到第一个通知。

答: 暂无答案