Prasanna Prasanna - 5 months ago 68
Swift Question

How to set default realm path to App Groups directory

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?

Answer

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
Comments