3

I am trying to set the default Realm path to App Groups directory.

let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("groups.prasanna.appName")!  
RLMRealm.setDefaultRealmPath(directory.absoluteString!)  
println(RLMRealm.defaultRealmPath())  

The app crashes with the following error

Terminating app due to uncaught exception 'RLMException', reason: 'open() failed: Operation not permitted'

How do I fix this issue?

Prasanna
  • 688
  • 1
  • 9
  • 27

4 Answers4

3

the default realm path you're setting is your container directory. You'll have to append a file name for this to work:

let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("groups.prasanna.appName")!
let realmPath = directory.path!.stringByAppendingPathComponent("db.realm")
RLMRealm.setDefaultRealmPath(realmPath)
println(RLMRealm.defaultRealmPath()) // should be realmPath
Doug
  • 794
  • 7
  • 12
jpsim
  • 14,329
  • 6
  • 51
  • 68
  • Thanks @jpsim. That gives me a file URL now. But the app crashes with the same exception nevertheless. – Prasanna Dec 09 '14 at 03:29
  • Try writing an empty file with NSFileManager to see if you have sufficient permissions: `NSFileManager.defaultManager().createFileAtPath(path, contents: nil, attributes: nil)`. The app group may not have been created properly. – jpsim Dec 09 '14 at 20:20
  • 1
    The issue was I need to use directory.path! rather than absoluteString. – Prasanna Dec 11 '14 at 09:18
  • How would you achieve this in iOS9 / Swift 2.1? – Lachtan Jan 29 '16 at 01:41
  • just change the second line to this: `let realmPath = directory.URLByAppendingPathComponent("db.realm").path!` – jpsim Jan 29 '16 at 15:58
1

RLMRealm.setDefaultRealmPath() removed at 0.97 version, you should use this : Tim answer

var config = RLMRealmConfiguration.defaultConfiguration()
config.path = realmPath
RLMRealmConfiguration.setDefaultConfiguration(config)
Amir Khorsandi
  • 3,542
  • 1
  • 34
  • 38
0

It has changed again, now:

let configuration = RLMRealmConfiguration.default()
configuration.pathOnDisk = realmPath
RLMRealmConfiguration.setDefault(configuration)
pesch
  • 1,976
  • 2
  • 17
  • 29
0

In Xamarin, you can do somethings like this to change default configuration path of Realm from Document to Library directory for Xamarin iOS app:

// Get path of Library directory first

var directoryLib = Environment.GetFolderPath(Environment.SpecialFolder.Resources);

//Configure your own path

var myOwnRealmPath = Path.Combine(directoryLib, "boards.realm");
RealmConfiguration.GetPathToRealm(myOwnRealmPath);

// Change default configuration path to your own (Here I have changed to Library directory)

RealmConfiguration.DefaultConfiguration = new RealmConfiguration(myOwnRealmPath);

//Get Realm Instance from your own designed path

_realm = Realm.GetInstance(RealmConfiguration.DefaultConfiguration);
JJIqbal
  • 630
  • 1
  • 8
  • 23