美文网首页
CloudKit同步和 Today Extension 的 AP

CloudKit同步和 Today Extension 的 AP

作者: 阿吖嗄 | 来源:发表于2022-06-29 14:58 被阅读0次

    ios 的 extension 使用了APP group 存储数据导致同步数据失败
    可以看一下wwdc2019
    最后的CoreDataStack代码大概就是下面这样的

    import CoreData
    
    class CoreDataStack {
        // MARK: - Core Data stack
    
        static var persistentContainer: NSPersistentCloudKitContainer = {
            let container = NSPersistentCloudKitContainer(name: "data")
            let storeURL = URL.storeURL(for: "group.com.myapp", databaseName: "data")
            let storeDescription = NSPersistentStoreDescription(url: storeURL)
            storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.myapp")
            container.persistentStoreDescriptions = [storeDescription]
            container.loadPersistentStores(completionHandler: { (storeDescription, error) in
                if let error = error as NSError? {
                    fatalError("Unresolved error \(error), \(error.userInfo)")
                }
            })
            return container
        }()
        // MARK: - Core Data Saving support
    
        static func saveContext () {
            let context = persistentContainer.viewContext
            if context.hasChanges {
                do {
                    try context.save()
                } catch {
                    let nserror = error as NSError
                    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
                }
            }
        }
    }
    
    public extension URL {
        static func storeURL(for appGroup: String, databaseName: String) -> URL {
            guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
                fatalError("Shared file container could not be created.")
            }
    
            return fileContainer.appendingPathComponent("\(databaseName).sqlite")
        }
    }
    

    相关文章

      网友评论

          本文标题:CloudKit同步和 Today Extension 的 AP

          本文链接:https://www.haomeiwen.com/subject/eogtbrtx.html