如何使用Realm删除数据库?

时间:2016-04-13 09:58:47

标签: swift realm

我想删除领域的数据库。 我知道如何在Java上删除它,但我需要在swift上执行此操作

像这样(Java):

RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.deleteRealm(realmConfig);
realm = Realm.getInstance(realmConfig);

感谢。

2 个答案:

答案 0 :(得分:2)

如果您要删除Realm的文件

let manager = NSFileManager.defaultManager()
let realmPath = Realm.Configuration.defaultConfiguration.path as! NSString
let realmPaths = [
  realmPath as String,
  realmPath.stringByAppendingPathExtension("lock")!,
  realmPath.stringByAppendingPathExtension("log_a")!,
  realmPath.stringByAppendingPathExtension("log_b")!,
  realmPath.stringByAppendingPathExtension("note")!
]
for path in realmPaths {
  do {
    try manager.removeItemAtPath(path)
  } catch {
    // handle error
  }
}

来自Realm的官方文档:
https://realm.io/docs/swift/latest/#deleting-realm-files

答案 1 :(得分:0)

最好的办法是在领域对象上调用[_locManager startUpdatingLocation]; if ([_locManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) { _locManager.allowsBackgroundLocationUpdates =YES; } 方法,如:

deleteAll()