我已实现NSPersistentCloudKitContainer将我的数据同步到CloudKit,我想知道同步已完成并且没有其他待同步的更改。
当我尝试重新安装该应用程序时,我开始从 CloudKit 取回我的数据并开始在控制台中打印某些日志。从 CloudKit 取回所有数据大约需要 30 秒。一些日志提到了NSCloudKitMirroringDelegate. 看起来NSCloudKitMirroringDelegate知道剩余的同步请求,但我找不到任何关于确保同步完成的信息。
这里有一些日志确实表明 NSCloudKitMirroringDelegate 知道同步何时完成。
CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate checkAndExecuteNextRequest::检查待处理的请求。
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke(714): : 入队请求: A2BB21B3-BD1B-4500-865C-6C848D67081D
CoreData:CloudKit:CoreData+CloudKit:-[NSCloudKitMirroringDelegate checkAndExecuteNextRequest]_block_invoke(2085)::推迟额外的工作。还有一个活动请求:A3E1D4A4-2BDE-4E6A-8DB4-54C96BA0579E
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndExecuteNextRequest]_block_invoke(2092): :没有更多的请求要执行。
有什么办法可以知道数据完全同步了吗?我需要向用户显示某些 UI。
Apple 在NSPersistentCloudKitContaineriOS 13 中引入了,这使我们能够将 CloudKit 与 Core Data 一起使用。我让它在不同的设备上几乎立即工作,但我的主要问题仍然存在。
是否可以以简单的方式与其他用户共享数据?我一直在阅读,CKShare但不知道如何NSPersistentCloudKitContainer以简单的方式从这里开始。
core-data ios cloudkit ckshare nspersistentcloudkitcontainer
我正在尝试在我的 CoreData+CloudKit 项目中执行历史跟踪,该项目使用NSPersistentCloudKitContainer. 我一直在关注 Apple 的示例项目
我想在远程存储更新后执行某些任务。为此,苹果建议在应用程序的签名和功能的后台模式部分启用远程通知。
我已经为我的项目启用了历史跟踪,如 Apple 的示例项目所示。
// turn on persistent history tracking
let description = container.persistentStoreDescriptions.first
description?.setOption(true as NSNumber,
forKey: NSPersistentHistoryTrackingKey)
// ...
Run Code Online (Sandbox Code Playgroud)
我还注册了我的商店来监听商店的变化。
// turn on remote change notifications
let remoteChangeKey = "NSPersistentStoreRemoteChangeNotificationOptionKey"
description?.setOption(true as NSNumber,
forKey: remoteChangeKey)
// ...
Run Code Online (Sandbox Code Playgroud)
还添加了观察者来侦听NSPersistentStoreRemoteChangeNotification.
然而,没有NSPersistentStoreRemoteChangeNotification被解雇。为了确保我的实现没有错误,我只是在@objc func storeRemoteChange(_ notification: Notification)Apple 提供的示例代码中放置了断点,但我仍然看不到任何通知被触发并且没有断点被激活。
我已经了解示例项目中完成的标签重复数据删除,也尝试对其进行测试,但没有任何成功。这是 Apple 实现中的错误还是我缺少任何必需的设置?
我正在向我现有的应用程序添加 Today Extension。我添加了一个应用程序组并使用这篇文章成功地将我的核心数据的数据迁移到了应用程序组的商店。我的应用程序同时使用 NSPersistentCloudKitContainer(当 iCloud 开启时)和一个 NSPersistentContainer(iCloud 关闭时)。虽然两个容器中的数据都成功迁移,但在使用 NSPersistentCloudKitContainer 时,我无法再在我的设备之间同步。在控制台中,我收到以下两个错误:
CoreData: 错误: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(837): : 无法为商店设置 CloudKit 集成: (URL: file://path/name.sqlite)
错误域=NSCocoaErrorDomain 代码=134060 “发生核心数据错误。” UserInfo={NSLocalizedFailureReason=镜像委托无法初始化,因为它的存储已从协调器中删除。}
第一条错误消息的路径是切换到 App Groups 之前的 oldURL 的路径。所以我相信我只需要告诉 iCloud 不要尝试在该商店位置集成 CloudKit 并使用应用程序组的商店位置。
但我不知道如何做到这一点。任何人都可以帮忙吗?
核心数据代码:
class CoreDataManager {
static let sharedManager = CoreDataManager()
private init() {}
lazy var persistentContainer: NSPersistentContainer = {
var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")
//Get the correct container
let containerToUse: NSPersistentContainer?
if useCloudSync {
containerToUse = NSPersistentCloudKitContainer(name: "App")
} else {
containerToUse …Run Code Online (Sandbox Code Playgroud) xcode core-data swift ios-app-group nspersistentcloudkitcontainer
我已经设法让 CoreData 与 CloudKit 一起使用我的应用程序的 iOS 版本上的新 NSPersistentCloudKitContainer 并让它在应用程序运行时自动同步。但是,当我在 watchOS 应用程序上进行设置时,我注意到只有在我强制关闭并重新打开手表应用程序时才会发生同步。
import Foundation
import CoreData
class DataManager : NSObject {
static let shared = DataManager()
#if os(watchOS)
let transactionAuthorName = "watchOSApp"
#else
let transactionAuthorName = "iOSApp"
#endif
override private init() {
super.init()
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "Model")
let cloudStoreUrl = applicationDocumentDirectory()!.appendingPathComponent("product.sqlite")
let cloudStoreDescription = NSPersistentStoreDescription(url: cloudStoreUrl)
cloudStoreDescription.shouldInferMappingModelAutomatically = true
cloudStoreDescription.shouldMigrateStoreAutomatically = true
cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier:"iCloud.com.company.product")
container.persistentStoreDescriptions = [cloudStoreDescription]
container.loadPersistentStores(completionHandler: { storeDescription, error in …Run Code Online (Sandbox Code Playgroud) core-data ios cloudkit watchos-6 nspersistentcloudkitcontainer
当我检查时Used with CloudKit,错误Folder.children must not be ordered出现了。关于有序关系的任何想法?
IDE:Xcode11 beta3
这是Folder实体。
这是我在 Apple 论坛上创建的帖子的副本
\n我在 App Store 中有一个已建立的应用程序,NSPersistentCloudKitContainer自 iOS 13 以来一直在使用,没有出现任何问题。\n我一直在运行 iOS 15 Beta 版的 iOS 设备上正常运行我的应用程序,主要是为了在用户看到问题之前发现问题.\n自从iOS 15 beta 4以来,我的应用程序就无法同步更改 - 无论更改有多小。上传“开始”但从未完成。大约一分钟后,应用程序退出到主屏幕,并且无法从崩溃报告中收集到任何有用的信息。直到现在我还不知道发生了什么事。
在 iOS 15 (beta 5) 上使用 Xcode 13 (beta 5) 构建应用程序时,我已成功在模拟器和另一台设备上复制此行为。\n似乎存在NSPersistentCloudKitContainer内存泄漏并不断增加 RAM 消耗(CPU 利用率为 100%)直到操作系统终止应用程序。我的代码没有运行。\n我并不是这些方面的真正专家,我尝试使用 Instruments 来看看是否可以向我显示任何内容。它似乎与以某种方式“卡住”有关NSCloudkitMirroringDelegate,但我不知道如何处理这些信息。\n我的核心数据数据库并不小,但无论如何也不大,NSPersistentCloudKitContainer同步到 iCloud 之前没有任何问题iOS 15(beta 4)。\n如果我恢复我的应用程序数据(从外部备份文件 - 700MB,包含大量多对多、多对一关系、ckAssets 等),所有数据都会毫无问题地添加到核心数据中根本不。然后控制台日志(见下文)显示同步已创建、计划并启动...但没有上传数据。\n此时内存消耗开始,我看到的只是出现“backgroundTask\”警告(仅与 CloudKit 相关),没有运行我的代码。
CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExporter analyzeHistoryInStore:withManagedObjectContext:error:](501): <PFCloudKitExporter: 0x600000301450>: Exporting changes since (0): <NSPersistentHistoryToken - {\n …Run Code Online (Sandbox Code Playgroud) 我NSPersistentCloudKitContainer在我的 iOS 应用程序中使用数据库。默认情况下,它似乎将所有事件记录到控制台,我假设是出于调试目的。这些消息如下所示:
CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExportContext processAnalyzedHistoryInStore:inManagedObjectContext:error:]_block_invoke_4(201): Finished processing analyzed history with 1 metadata objects to create, 0 deleted rows without metadata.
CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExporter exportIfNecessary]_block_invoke_2(137): <PFCloudKitExporter: 0x282a785f0>: Found 2 objects needing export.
CoreData: warning: CoreData+CloudKit: -[PFCloudKitExporter exportOperationFinished:withSavedRecords:deletedRecordIDs:operationError:](384): Modify records finished: (
etc...
Run Code Online (Sandbox Code Playgroud)
它们是恒定的并且妨碍了其他调试消息,有什么方法可以禁用它们?
我刚刚添加了一个选项,供用户在我的应用程序中切换云同步,我保存用户是否要在“useCloudSync”下的 UserDefaults 中使用 iCloud 同步。当应用程序运行时,我加载我的persistentContainer:
class CoreDataManager {
static let sharedManager = CoreDataManager()
private init() {}
lazy var persistentContainer: NSPersistentContainer = {
var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync")
let containerToUse: NSPersistentContainer?
if useCloudSync {
containerToUse = NSPersistentCloudKitContainer(name: "App")
} else {
containerToUse = NSPersistentContainer(name: "App")
let description = containerToUse!.persistentStoreDescriptions.first
description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
}
guard let container = containerToUse, let description = container.persistentStoreDescriptions.first else {
fatalError("Hey Listen! ###\(#function): Failed to retrieve a persistent store description.")
}
description.setOption(true as NSNumber, forKey: …Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用NSPersistentCloudKitContainer. 该应用程序没有共享功能,其唯一的后端功能是使用 cloudkit 容器在用户设备之间同步数据。设置相当简单,实例化容器,设置单个商店描述,然后加载商店。
我的大问题:我需要对持久历史记录进行任何操作吗?我尚未找到这个问题的具体答案,但据我所知,持久历史记录跟踪用于将一个目标(例如扩展)中发生的更改合并到另一个目标中。听起来我不需要它来充分利用 iCloud 同步。