How to find Realm file location of a Mac App

人盡茶涼 提交于 2019-12-12 12:24:17

问题


First of all, I am very new to Realm ecosystem.

I have tried this in AppDelegate's applicationDidFinishLaunching method,

print("Realm Path : \(realm.configuration.fileURL?.absoluteURL)")

But it gave me error because, I have added some new properties to the realm object, which produced conflicts with the old schema and crashed the app before even printing this.

If you are interested in the crash log. But it has nothing to do with the main question, right?

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'id' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'id' has been added to latest object model., Error Code=10}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.8/src/swift/stdlib/public/core/ErrorType.swift, line 54

I have one more doubt. I have added this, the realm has to deleteAll before even initialising the old objects right? But it just crashed with the log given above.

import Cocoa
import RealmSwift
import Realm

//MARK: Global Variables

public let realm = try! Realm()

func applicationDidFinishLaunching(aNotification: NSNotification) {
   realm.deleteAll()
   print("Realm Path : \(realm.configuration.fileURL?.absoluteURL)")
}

So now I want to delete the realm file, but I have no idea where to find it. I have searched in Mac using extension .realm, but could not the find the expected file. I have the Realm browser app, I thought it would redirect me to the file location, but it didn't, it just opened the Documents folder, there is no realm file.

How would you do it? Thanks it advance.


回答1:


The Realm does change when you create new file.

Just add the following line to your ViewDidLoad method:

print(Realm.Configuration.defaultConfiguration.fileURL!)

This line will print the location to XCode console. Copy the file path, go to Finder -> Go -> Go go Folder... -> paste the path and hit Go.




回答2:


I found the answer to my own question, Happiness :) It will be in this directory.

Users/UserName/Library/Application Support/appBundleIdentifer/default.realm)

Basically username stands for your computer's main users name, and the bundle identifier stands for something like this in your project com.yourCompany.yourApp



来源:https://stackoverflow.com/questions/38279615/how-to-find-realm-file-location-of-a-mac-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!