In some cases, you may want to completely delete a realm file from disk.
Realm avoids copying data into memory except when absolutely required. As a result, all objects managed by a realm have references to the file on disk. Before you can safely delete the file, you must ensure the deallocation of these objects:
All objects read from or added to the realm
All List and Results objects
All ThreadSafeReference objects
The realm itself
If you delete a realm file or any of its auxiliary files while one or more instances of the realm are open, you might corrupt the realm or disrupt sync.
If you iterate rapidly as you develop your app, you may want to delete a realm file instead of migrating it when you make schema changes. The Realm configuration provides a deleteRealmIfMigrationNeeded parameter to help with this case.
When you set this property to true
, the SDK deletes the realm file when a migration would be required. Then, you can create objects that match the new schema instead of writing migration blocks for development or test data.
do { let configuration = Realm.Configuration(deleteRealmIfMigrationNeeded: true) let realm = try Realm(configuration: configuration)} catch { print("Error opening realm: \(error.localizedDescription)")}
In practice, there are two safe times to delete the realm file:
On application startup before ever opening the realm.
After only having opened the realm within an explicit autorelease
pool, which ensures deallocation of all of objects within it.
You can delete the .realm
, .note
and .management
files for a given configuration with the +[RLMRealm deleteFilesForConfiguration:error:] class method.
@autoreleasepool { }RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];RLMUser *user = [app currentUser];RLMRealmConfiguration *configuration = [user configurationWithPartitionValue:@"some partition value"];configuration.objectClasses = @[Task.class];NSError *error = nil;[RLMRealm deleteFilesForConfiguration:configuration error:&error];if (error) { }
You can delete the .realm
, .note
and .management
files for a given configuration with the Realm.deleteFiles(for:) class method.
autoreleasepool { }do { let app = App(id: APPID) let user = try await app.login(credentials: Credentials.anonymous) var configuration = user.flexibleSyncConfiguration() _ = try Realm.deleteFiles(for: configuration)} catch { }
When you use Device Sync, you may encounter a client reset error. During a client reset, your app must delete the local copy of the realm and download an updated version from the Atlas App Services backend.
Because Synced realms are stored locally and can be used offline, deleting a Synced realm could lose data. If the client has written to the realm and the realm has not uploaded those changes, the client loses that data when you delete the Synced realm. The realm may be unable to upload changes when the client doesn't have a network connection for a long period of time, or in the event of a Sync error that requires you to restart Sync.
For more details on how to handle this scenario, see Client Reset.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4