我设置了一个应用程序组与我的 Today Extension 一起使用。我在主应用程序的目标和扩展程序的目标中添加了应用程序组。在开发人员门户的应用程序 ID 配置中,我启用了应用程序组。但由于某种原因FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: id)
每当我调用它时都返回零。我已经检查了三次forSecurityApplicationGroupIdentifier
字符串并知道它是正确的。
有谁知道为什么这不起作用?
编辑:我能够通过编辑我的调试权利以包含应用程序组来解决零问题。我一直在关注这个post https://stackoverflow.com/questions/52191523/ios-11-how-to-migrate-existing-core-data-to-shared-app-group-for-use-in-extensi并能够成功地从 NSPersistentContainer 迁移我的数据,但是当我尝试在用户打开 iCloud 时使用 NSPersistentCloudKitContainer 时,同样的方法不起作用。它仍然能够迁移数据,但不允许我在设备之间同步数据(几乎只是使其成为常规的 NSPersistentContainer)。如果我恢复到以前的方式,我就可以使用 iCloud 同步。
任何人都可以帮我解决迁移时的同步问题吗?NSPersistentCloudKitContainer
使用应用程序组?
核心数据代码:
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 = NSPersistentContainer(name: "App")
}
guard let container = containerToUse else {
fatalError("Couldn't get a container")
}
//Set the storeDescription
let storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.App")!.appendingPathComponent("\(container.name).sqlite")
var defaultURL: URL?
if let storeDescription = container.persistentStoreDescriptions.first, let url = storeDescription.url {
defaultURL = FileManager.default.fileExists(atPath: url.path) ? url : nil
}
if defaultURL == nil {
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: storeURL)]
}
let description = container.persistentStoreDescriptions.first else {
fatalError("Hey Listen! ###\(#function): Failed to retrieve a persistent store description.")
}
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
if !useCloudSync {
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
//migrate from old url to use app groups
if let url = defaultURL, url.absoluteString != storeURL.absoluteString {
let coordinator = container.persistentStoreCoordinator
if let oldStore = coordinator.persistentStore(for: url) {
do {
try coordinator.migratePersistentStore(oldStore, to: storeURL, options: nil, withType: NSSQLiteStoreType)
} catch {
print("Hey Listen! Error migrating persistent store")
print(error.localizedDescription)
}
// delete old store
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
fileCoordinator.coordinate(writingItemAt: url, options: .forDeleting, error: nil, byAccessor: { url in
do {
try FileManager.default.removeItem(at: url)
} catch {
print("Hey Listen! Error deleting old persistent store")
print(error.localizedDescription)
}
})
}
}
}
return container
}
}
我认为问题出在商店描述上,但是当我查看更改前后的 storeDescription 时,它的选项仍然打开了 iCloud 同步。