11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Realmのデータベースをドロップする

Last updated at Posted at 2016-04-14

モデルを変更するたびにマイグレーションで怒られるのに疲れたのでアプリを起動するたびにRealmのデータベースをドロップすることにした。

アプリ起動時に以下のコードを呼び出してあげればOK。

// Drop database
let path = Realm.Configuration.defaultConfiguration.path!
if NSFileManager.defaultManager().fileExistsAtPath(path) {
    try! NSFileManager.defaultManager().removeItemAtPath(path)
}

追記

Realm1.0になってAPIが変更になり上記のコードは使えなくなってた。Realm公式ドキュメントによると以下のようにすれば良いみたい。

let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
  let realmURLs = [
  realmURL,
  realmURL.URLByAppendingPathExtension("lock"),
  realmURL.URLByAppendingPathExtension("log_a"),
  realmURL.URLByAppendingPathExtension("log_b"),
  realmURL.URLByAppendingPathExtension("note")
]
let manager = NSFileManager.defaultManager()
for URL in realmURLs {
  do {
    try manager.removeItemAtURL(URL)
  } catch {
    // handle error
  }
}

参考: https://realm.io/jp/docs/swift/latest/#realm-7

参考

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?