Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely remove Realm database from iOS?

Tags:

swift

realm

Now I get the error Property types for 'value' property do not match. Old type 'float', new type 'double'. How can I clear the database or migrate it successfully?

like image 692
Vassily Avatar asked May 23 '16 16:05

Vassily


People also ask

How do you delete a realm database?

try { Realm. deleteRealmFile(getActivity()); //Realm file has been deleted. } catch (Exception ex){ ex. printStackTrace(); //No Realm file to remove. }

Where is my realm file IOS?

Open Finder in that folder: open ~/Library/Developer/CoreSimulator/Devices and then create a "saved search" so that you can always find all of your realm files.

What is realm database IOS?

Realm is a fast, scalable alternative to SQLite with mobile to cloud data sync that makes building real-time, reactive mobile apps easy.


1 Answers

To completely delete the Realm file from disk and start from scratch, it's simply a matter of using NSFileManager to manually delete it.

For example, to delete the default Realm file:

NSFileManager.defaultManager().removeItemAtURL(Realm.Configuration.defaultConfiguration.fileURL!)

If you want to preserve the Realm file, but completely empty it of objects, you can call deleteAll() to do so:

let realm = try! Realm()
try! realm.write {
  realm.deleteAll()
}

Update: I feel I neglected to mention this in my original answer. If you choose to delete the Realm file from disk, you must do so before you've opened it on any threads in your app. Once it's opened, Realm will internally cache a reference to it, which won't be released even if the file is deleted.

If you absolutely do need to open the Realm file to check its contents before deletion, you can enclose it in an autoreleasepool to do this.

like image 179
TiM Avatar answered Oct 22 '22 08:10

TiM