提问人:Petr Smejkal 提问时间:11/3/2023 最后编辑:jnpdxPetr Smejkal 更新时间:11/3/2023 访问量:58
向用户显示iCloud同步状态
Display iCloud sync status to user
问:
我正在将 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)
}
}
使用此代码时,将设置为同步过程的最后阶段(最后一秒或两秒),而控制台显示工作正在完成更长的时间。有什么方法可以“更快”通知用户吗?isSyncing
true
答: 暂无答案
评论
Is there any way to notify the user "sooner"?
,早于何时?比设置iCloud同步时更快?或者您是关于如何显示 ProgressView() 的问题?