How to set default realm path to App Groups directory

拈花ヽ惹草 提交于 2019-12-10 10:44:42

问题


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?


回答1:


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



回答2:


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

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



回答3:


It has changed again, now:

let configuration = RLMRealmConfiguration.default()
configuration.pathOnDisk = realmPath
RLMRealmConfiguration.setDefault(configuration)



回答4:


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);


来源:https://stackoverflow.com/questions/27355557/how-to-set-default-realm-path-to-app-groups-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!