【问题标题】:Core Data file's Location iOS 10Core Data 文件的位置 iOS 10
【发布时间】:2017-02-04 23:28:32
【问题描述】:

我正在尝试使用 SQLite 浏览器查看我的核心数据对象。我无法找到核心数据将其 sql 文件保存在哪里。我查看了应用程序文档文件夹,但那里什么也没有。

你知道IOS 10(模拟器)中的核心数据在哪里保存它的SQLite文件吗?

【问题讨论】:

    标签: ios swift core-data ios-simulator ios10


    【解决方案1】:

    我在 XCode 8 Swift 3 OS macOS Sierra 上对其进行了测试

    IOS 10

    在您的 Appdelegate.swif 中t

    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)
    
        return true
    }
    

    1.常量.documentDirectory 表示我们正在寻找文档目录

    2.常量.userDomainMask 将我们的搜索限制在应用程序的沙箱中。

    输出

    file:///Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Data/Application/3B373C93-71A2-46E9-8AF7-EF407C39429F/Documents/
    

    点击前往 -> 前往文件夹 -> 粘贴路径 -> 按 Enter

    然后转到库 -> 应用程序支持 -> 文件名.sqlite

    已编辑

    打开您的终端并输入 find ~ -name 'HitTest.sqlite' 并回车。

    Ashoks-MacBook-Pro:Desktop Ashok$ find ~ -name 'HitTest.sqlite'
    /Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Data/Application/3B373C93-71A2-46E9-8AF7-EF407C39429F/Documents/HitTest.sqlite
    

    从上面的输出你可以清楚地看到你的 sqlite db 的路径

    您可以使用DB Browser for SQLite打开。

    【讨论】:

      【解决方案2】:

      试试这个,我没有在 ios 10 上检查过,但它在以前的所有版本中都可以工作

      产品 > 方案 > 编辑方案 > 运行 > 参数

      在“Arguments Passed on launch”中添加此参数

      -com.apple.CoreData.SQLDebug 1

      每次应用程序启动时,它都会打印到数据库的路径 See This argument look like this

      【讨论】:

      • 感谢您的回复。我试过这个,但它不起作用。我把参数放在启动时传递的参数下,-com.apple.CoreData.SQLDebug 1,但这不会打印任何东西。你知道为什么吗?
      • 这是一个更好的选择,而不是查找文件路径,因为如果您的应用程序已共享 Db(带有扩展名或其他组应用程序),那么它将与文件路径不在同一位置,而是在共享位置.所以最好用这种方式来查找sql​​ite db的位置。
      • 此设置适用于 iOS 11 和 Xcode 9。DB 文件仍位于 /Library/Application Support/ 目录中。
      【解决方案3】:

      Swift 3.x

      在您的 AppDelegate 文件中找到 didFinishLaunchingWithOptions 并添加这一行。

      let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
      print("\(path)")
      

      【讨论】:

        【解决方案4】:

        对于 swift 5

        func getCoreDataDBPath() {
                let path = FileManager
                    .default
                    .urls(for: .applicationSupportDirectory, in: .userDomainMask)
                    .last?
                    .absoluteString
                    .replacingOccurrences(of: "file://", with: "")
                    .removingPercentEncoding
        
                print("Core Data DB Path :: \(path ?? "Not found")")
            }
        

        这将给出直到 sqlite 文件 (dbname.sqlite) 的详细路径

        print("CoreData path :: \(self.getCoreDataDBPath())")
        

        注意:点击Go -> Go to Folder -> Paste path here -> Enter

        【讨论】:

          【解决方案5】:

          设置后:

          // available for iOS/iPadOS/Catalyst/macOS, other systems are not tested yet
          context.persistentStoreCoordinator?.persistentStores.forEach({
              if let url = $0.url {
                  print(url)
              }
          })
          

          【讨论】:

          • 有一个lazy var persistentContainer: NSPersistentContainer 我的代码看起来有点不同:persistentContainer.persistentStoreCoordinator.persistentStores.forEach({ if let url = $0.url { print(url) } })
          【解决方案6】:

          由于您的应用程序的核心数据文件的位置是可变的,找到一个后,您可以使用 SQLite 浏览器,如SQLiteFlow 来浏览您的核心数据文件的内容。原因很简单,它可以处理 SQLite 文件位置的变化。来自 SQLiteFlow 的文档:

          处理数据库文件名或目录更改。这使得 SQLiteFlow 可以在 iOS 模拟器中与您的 SQLite 数据库友好地工作。

          【讨论】:

            【解决方案7】:

            对于 XCODE 8.0 (iOS 10):-

            .sqlite 持久存储的路径是:

            /Users/Ashish/Library/Developer/CoreSimulator/Devices/"YourDeviceId"/data/Containers/Data/Application/"Application Id"/Library/Application Support/"FileNamePassedInPersistentContainer.sqlite"

            【讨论】:

              【解决方案8】:

              对于 Swift v5.x,这将起作用。

              func getCoreDataDBPath() {
                      let path = FileManager
                          .default
                          .urls(for: .applicationSupportDirectory, in: .userDomainMask)
                          .last?
                          .absoluteString
                          .replacingOccurrences(of: "file://", with: "")
                          .removingPercentEncoding
              
                      print("Core Data DB Path :: \(path ?? "Not found")")
                  }
              

              打印此路径后,您可以使用 DB Browser for SQL Lite 查看所有表。

              【讨论】:

                猜你喜欢
                • 2016-11-20
                • 2017-05-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-01-17
                • 1970-01-01
                相关资源
                最近更新 更多