Robert Byrne Robert Byrne - 3 months ago 19
iOS Question

Issue finding default realm file in RealmSwift iOS 7 Swift 2

I am having a major issue when trying to integrate realm into a project with Swift 2 in Xcode 7. I am trying to find the default.realm file so that I can usually see the DB in Realm Browser.

So far I have scoured the internet and tried the following solutions...

How to find my realm file?

Solution option 1:

Tried printing the file path to the console, comes up with errors that I cannot solve, used the following print commands

print(Realm().path)


and then...

let realm = Realm(path: "/Users/me/Desktop/TestRealm.realm")


Solution option 2:

Tried pausing the simulator and putting this into the LLDB console...


po Realm.defaultPath


which returns...


error: :1:1: error: use of unresolved identifier 'Realm'
Realm.defaultPath


For reference here are the files that create the realm objects

import UIKit
import RealmSwift

class XMCMovie: Object {
dynamic var id = ""
dynamic var title = ""
dynamic var tomatometer = 0
dynamic var consensus = ""
dynamic var imageName = ""

override class func primaryKey() -> String? {
return "id"
}

required init() {
super.init()
}

init(id: NSString, title: NSString, tomatometer: Int, consensus: NSString, imageName: NSString) {
super.init()

self.id = id as String
self.title = title as String
self.tomatometer = tomatometer
self.consensus = consensus as String
self.imageName = imageName as String
}
}

import UIKit
import RealmSwift

class XMCApi {

class func requestOpeningMovies() {
let movies = [ XMCMovie(id: "0", title: "The Hobbit: The Battle Of The Five Armies", tomatometer: 62, consensus: "Suitably grim, epic, and action-packed, The Hobbit: The Battle of the Five Armies ends Peter Jackson's second Middle-earth trilogy on a rousing high note.", imageName: "hobbit"),
XMCMovie(id: "1", title: "Night At The Museum: Secret Of The Tomb", tomatometer: 53, consensus: "No consensus yet.", imageName: "museum"),
XMCMovie(id: "2", title: "Annie", tomatometer: 20, consensus: "The new-look Annie hints at a progressive take on a well-worn story, but smothers its likable cast under clich├ęs, cloying cuteness, and a distasteful materialism.", imageName: "annie"),
XMCMovie(id: "3", title: "Mr. Turner", tomatometer: 97, consensus: "Led by a masterful performance from Timothy Spall and brilliantly directed by Mike Leigh, Mr. Turner is a superior Hollywood biopic.", imageName: "turner"),
XMCMovie(id: "4", title: "Song Of The Sea", tomatometer: 100, consensus: "No consensus yet.", imageName: "sea") ]

// Write our movie objects to the database
let realm = try! Realm()

try! realm.write() {

for movie in movies {
/* This method will avoid duplicating records by looking at the
primary key we've set on our object. Go look at the XMCMovie
class to see that method defined.
*/

// XMCMovie.createOrUpdateInDefaultRealmWithObject(movie)

// Alternatively, you could add new objects by calling this method
realm.add(movie)
// or
// realm.addObjects(movies) // An array of objects
}

}
}
}


If anyone has any guidance about where to find a solution to this problem that would be wonderful. Thanks!

-RB

Answer

After a little tweaking, I was able to find the answer. So I will post the print command that worked for me. Note: this is in Swift 2.1.1

print(Realm.Configuration.defaultConfiguration.path!)

Cheers,

-RB