3
3

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.

Swift:脱Realmする方法。

Posted at

データベースを利用するためにRealmを使っていたのですが、よく考えるとRealmを使うほどでもない情報の保存しかしていないことに気づき、脱Realmしようと思い至りました。
そこで、Realmのデータを完全に削除する方法について調べると、公式リファレンスにて

let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
  let realmURLs = [
  realmURL,
  realmURL.appendingPathExtension("lock"),
  realmURL.appendingPathExtension("note"),
  realmURL.appendingPathExtension("management")
]
let manager = NSFileManager.defaultManager()
for URL in realmURLs {
  do {
    try FileManager.default.removeItem(at: URL)
  } catch {
    // handle error
  }
}

こんな感じで、Realm.Configuration.defaultConfiguration.fileURLを用いてデータファイルを削除できるよって書いてありました。

つまり、脱RealmするにはRealmが必要ってこと?? ヤダー😭 となったのですが、要はこのURLをなんとか生成できればいいんでしょという方針で対策することにしました。

func destroyRealm() {
    guard let dir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
    else { return }
    let realmURLs: [URL] = [
        dir.appendingPathComponent("default.realm"),
        dir.appendingPathComponent("default.realm.lock"),
        dir.appendingPathComponent("default.realm.note"),
        dir.appendingPathComponent("default.realm.management")
    ]
    for url in realmURLs {
        try? FileManager.default.removeItem(at: url)
    }
}

これで、同等のURLを指定してファイルやディレクトリを削除することができるはずです。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?