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
Advertencia
No borrar archivos mientras los reinos estén abiertos
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.
Borrar un archivo Realm para evitar la migración
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.
Cuando se establece esta propiedad en trueEl SDK elimina el archivo de dominio cuando se requiere una migración. De esta manera, puede crear objetos que coincidan con el nuevo esquema en lugar de escribir bloques de migración para datos de desarrollo o prueba.
do { // Delete the realm if a migration would be required, instead of migrating it. // While it's useful during development, do not leave this set to `true` in a production app! let configuration = Realm.Configuration(deleteRealmIfMigrationNeeded: true) let realm = try Realm(configuration: configuration) } catch { print("Error opening realm: \(error.localizedDescription)") }
Delete a Realm File
En la práctica, hay dos momentos seguros para borrar el archivo Realm:
On application startup before ever opening the realm.
Después de haber abierto únicamente el realm dentro de un pool
autoreleaseexplícito, que asegure la liberación de todos los objetos dentro del mismo.
You can delete the .realm, .note and .management files for a given configuration with the +[RLMRealm deleteFilesForConfiguration:error:] class method.
@autoreleasepool { // all Realm usage here -- explicitly guarantee // that all realm objects are deallocated // before deleting the files } // Get configuration 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; // Delete realm files for that configuration [RLMRealm deleteFilesForConfiguration:configuration error:&error]; if (error) { // Handle error }
Puede eliminar los .realm .note .management archivos, y de una configuración determinada con el método de clase Realm.deleteFiles(for:).
autoreleasepool { // all Realm usage here -- explicitly guarantee // that all realm objects are deallocated // before deleting the files } 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 { // handle error }
Eliminar un Archivo Realm Durante un Restablecimiento del Cliente
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.
Para obtener más detalles sobre cómo gestionar este escenario, consulta restablecimiento del cliente.