5

I have added the Realm.framework and RealSwift.framework to a project. and "import Realm" though I'm getting this error:

RLMRealm' has no member 'setDefaultRealmPath'

 let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.it.fancypixel.Done")!
        let realmPath = (directory.path! as NSString).stringByAppendingPathComponent("db.realm")
        RLMRealm.setDefaultRealmPath(realmPath)

Any ideas I can't seem to see a solution for this anywhere being it's so new.

Thanks in advance.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
uplearned.com
  • 3,393
  • 5
  • 44
  • 59

3 Answers3

10

Realm (Both the Swift and Objective-C libraries) was just updated to version 0.97. While setDefaultRealmPath was a valid API in the past, it was subsequently deprecated, and as of 0.97, completely removed. As such, if it was working in the past, once you've updated to 0.97, it will now result in a build error.

Setting the file location of a Realm is now controlled via Realm RLMRealmConfiguration objects. To set the default path, you would now do it like this:

let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.it.fancypixel.Done")!
let realmPath = (directory.path! as NSString).stringByAppendingPathComponent("db.realm")

var config = RLMRealmConfiguration.defaultConfiguration()
config.path = realmPath
RLMRealmConfiguration.setDefaultConfiguration(config)

Let me know if you need any more clarification!

TiM
  • 15,812
  • 4
  • 51
  • 79
  • Ok great this is working. Thank you! By any chance can you help me understand how it syncs across devices. I added the realmToken = RLMRealm.defaultRealm().addNotificationBlock { note, realm in self.reloadEntries() } though it's registering a change on my other devices when adding a new realm object to another device. – uplearned.com Dec 23 '15 at 12:39
  • Awesome! Glad to hear it's working! No, the Realm notification blocks don't sync across devices; they're usually used for when you're modifying Realm objects on a background thread and want to be notified about it elsewhere in the app. However, syncing across devices has been brought up on our GitHub page before. :) – TiM Dec 24 '15 at 02:57
2

The new way to change the default Realm path:

let directory: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.YOUR_BUNDLE_ID")!
    let realmPath = directory.path.appending("db.realm")
    let configuration = RLMRealmConfiguration.default()
    configuration.pathOnDisk = realmPath
    RLMRealmConfiguration.setDefault(configuration)
pesch
  • 1,976
  • 2
  • 17
  • 29
1

In Swift 4 you can use this

let identifier = "group.companyName.projectName"
    var directory: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: identifier)!


    directory.appendPathComponent("db.realm", isDirectory: true)

    let config = Realm.Configuration(
        fileURL: directory,
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in  })

    Realm.Configuration.defaultConfiguration = config
Preeti Rani
  • 685
  • 7
  • 17