小编cr0*_*0ss的帖子

Swift3:通过AppGroups从Watch Extension访问Core Data时的空提取

目前我正在为已有的App(迁移到Swift 3)进行更新.我有针对Today-,Search-,Message-和Watch Extensions的目标.每个目标都需要访问我的应用程序的核心数据模型,因此我创建了一个AppGroup并为每个目标启用了Capability.虽然我已经将NSPersistentStoreCoordinator子类化,但所有内容都存储在共享文件夹中:

import CoreData
class PFPersistentContainer: NSPersistentContainer {
    override open class func defaultDirectoryURL() -> URL {
        if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "add-groupname-here") {
            return url
        }
        // Fallback
        return super.defaultDirectoryURL()
    }
}
Run Code Online (Sandbox Code Playgroud)

这个类文件,以及我的Core-Data-Model和下面的类都是所有提到的目标的目标成员.实体的Codegen设置为Class Definition.到目前为止,我正在使用Core Data Stack的默认实现:

class DataManager {
    /**
     * Singleton Implementation
     */
    internal static let sharedInstance:DataManager = {
        let instance = DataManager()
        return instance
    }()

    // MARK: - Core Data stack
    lazy var persistentContainer: PFPersistentContainer = {
        let container = PFPersistentContainer(name: "Data")
        container.loadPersistentStores(completionHandler: { (storeDescription, …
Run Code Online (Sandbox Code Playgroud)

core-data ios swift3 watchos-3 appgroups

7
推荐指数
1
解决办法
417
查看次数

具有预填充.sqlite的核心数据(Swift3)

目前我正在研究现有iOS9应用程序的Swift3/iOS10更新,该应用程序为欧洲各地的电动汽车存储约10,000个充电点.到目前为止,我总是使用预先填充的数据库(.xcappdata包中的.sqlite,.sqlite-shm,.sqlite-wal文件)发送应用程序,但是使用当前版本的Apple正在引入NSPersistentContainer类,这使得它成为可能.有点复杂.在我的AppDelegate类中,我实例化我的NSPersistentContainer对象并将其传递给一个懒惰的var,就像Apple在每个示例代码中所做的那样:

   lazy var stationDataPersistentContainer: NSPersistentContainer = {  
     let fileMgr = FileManager.default
     let destinationModel = NSPersistentContainer.defaultDirectoryURL()
     if !fileMgr.fileExists(atPath: destinationModel.appendingPathComponent("StationData.sqlite").path) {
         do {
             try fileMgr.copyItem(at: URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/StationData.sqlite")), to: destinationModel.appendingPathComponent("/StationData.sqlite"))
             try fileMgr.copyItem(at: URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/StationData.sqlite-shm")), to: destinationModel.appendingPathComponent("/StationData.sqlite-shm"))
             try fileMgr.copyItem(at: URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/StationData.sqlite-wal")), to: destinationModel.appendingPathComponent("/StationData.sqlite-wal"))
         } catch {
             //
         }
     } else {
         //
     }
     /* 
     The persistent container for the application. This implementation 
     creates and returns a container, having loaded the store for the 
     application to it. This property is optional since there …
Run Code Online (Sandbox Code Playgroud)

sqlite core-data swift swift3 ios10

3
推荐指数
1
解决办法
1784
查看次数

标签 统计

core-data ×2

swift3 ×2

appgroups ×1

ios ×1

ios10 ×1

sqlite ×1

swift ×1

watchos-3 ×1