Alvin Varghese Alvin Varghese - 5 months ago 105
Swift Question

How to find Realm file location of a Mac App

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.

Answer

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.