Search code examples
iosswiftrealm

How to delete Realm if I instantiate realm as a class property?


Based on the examples provided by the Realm team, I have instantiated the realm and bind it to a class variable called 'realm' like this:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let realm = try! Realm()

I think it makes sense and makes the code more readable. However, my project is in it's early stage, the objects are changing quite a lot and I am trying to avoid performing migrations with this:

Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true

Naturally this leads to trouble since I am trying to delete a Realm file after opening the Realm which is excplicitly forbidden in the docs. I have followed similar design pattern in my UIViewController classes as well, which makes refactoring a bit cumbersome.

The question is:
Is it bad practice to bind Realm instance to a class property as I have done? Should I always reference to Realm with let realm = try! Realm() block when I need to read my objects? Or is there some way to delete the Realm without deleting all the class wide realm instances?

Thank you 🙏


Solution

  • I use this pattern:

    Singleton DataManager which is used to manage Realm access. It can wrap all the trys, for example:

    class DataManager {
        static let shared = DataManager()
    
        func add(_ object: Object, update: Bool = true) {
            do {
                let realm = try Realm()
                try realm.write {
                    realm.add(object, update: update)
                }
            } catch {
                print (error)
            }
        }
    
        ...Other shared Realm methods...
    }
    

    I also have a MigrationManager singleton, which as the name suggests, handles Realm migrations. It's method get called very early - well before DataManager methods are called

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        ...
        MigrationManager.shared.migrate()
        ...
    }
    

    some MigrationManager code

    class MigrationManager {
        static let shared = MigrationManager()
    
        let schemaVersion: UInt64 = 3
    
        private var migrationBlock: MigrationBlock? {
            return { migration, oldSchemaVersion in
            ...
            }
        }
    
        func migrate() {
            let config = Realm.Configuration(
                schemaVersion: schemaVersion,
                migrationBlock: migrationBlock)
    
            Realm.Configuration.defaultConfiguration = config
        }
    }