【问题标题】:Location of Sqlite file location of CoreData iOS AppCoreData iOS App的Sqlite文件位置
【发布时间】:2016-02-11 16:59:49
【问题描述】:

我正在使用 Objective-C 和 CoreData 开发一个 iOS 应用程序。在哪里可以找到 iOS 设备上生成的 sqlite 文件?

【问题讨论】:

标签: ios objective-c sqlite core-data


【解决方案1】:

如果商店使用持久化文件,您甚至可以在代码中获取它

//get you context, the coordinator, attached stores
NSManagedObjectContext *moc = [MyDataStore sharedDataStore].mainManagedObjectContext; //however you get the MOC
NSPersistentStoreCoordinator *coordinator = moc.persistentStoreCoordinator;
NSArray<NSPersistentStore*> *stores = coordinator.persistentStores;

//log it
for (NSPersistentStore *store in stores) {
    NSLog(@"%@", store.URL);
}

【讨论】:

    【解决方案2】:

    适用于 XCode 8.0 (iOS 10)

    可以通过在 AppDelegate 的现有 (didFinishLaunchingWithOptions) 方法中添加以下代码来查看 NSPersistentContainer 创建的持久存储的路径:-

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    
        print(urls[urls.count-1] as URL) // look for doc. directory
    
        return true
    }
    

    转到打印日志中的路径,然后转到 /Library/Application Support 文件夹。这是 .sqlite(persistent store) 文件的示例路径:

    /Users/Ashish/Library/Developer/CoreSimulator/Devices/F04D26BC-0AA9-4E25-87CC-E635BE86293A/data/Containers/Data/Application/69873117-6AB0-4491-B3BA-302FE63050FB/Library/Application Support

    【讨论】:

      【解决方案3】:

      这是您决定放置的位置。您甚至不需要查找它,因为您已经掌握了这些信息。

      当您调用addPersistentStoreWithType:configuration:URL:options:error: 时,您传入的URL 参数是持久存储的位置。您可以通过NSManagedObjectContextNSPersistentStoreCoordinator 查找它,但是当您的应用中已经存在答案时,为什么还要这样做呢?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-20
        • 2019-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 1970-01-01
        相关资源
        最近更新 更多