如何将默认域路径设置为App Groups目录

时间:2014-12-08 10:06:23

标签: cocoa swift realm

我正在尝试将默认Realm路径设置为App Groups目录。

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

该应用程序因以下错误而崩溃

  

由于未捕获的异常“RLMException”而终止应用程序,原因:'open()失败:操作不被允许'

如何解决此问题?

4 个答案:

答案 0 :(得分:3)

您正在设置的默认域路径是容器目录。你必须附加一个文件名才能工作:

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

答案 1 :(得分:1)

在0.97版本中删除

RLMRealm.setDefaultRealmPath(),您应该使用:Tim answer

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

答案 2 :(得分:0)

现在又改变了,现在:

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

答案 3 :(得分:0)

在Xamarin中,您可以执行以下操作将Xamarin iOS应用的Realm默认配置路径从Document更改为Library目录:

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