It may be really difficult to completely demolish all the extracted model accessors, but unfortunately there is no other way to close the kingdom.
As you wrote โperiodicallyโ, each application launch can be quite often, depending on your use case.
When starting the application, it should be relatively easy to open Realm in the selected autoreleasepool, write a compressed copy to another path and replace the default.realm file with it.
Swift 2.1
func compactRealm() { let defaultURL = Realm.Configuration.defaultConfiguration.fileURL! let defaultParentURL = defaultURL.URLByDeletingLastPathComponent! let compactedURL = defaultParentURL.URLByAppendingPathComponent("default-compact.realm") autoreleasepool { let realm = try! Realm() realm.writeCopyToPath(compactedURL) } try! NSFileManager.defaultManager().removeItemAtURL(defaultURL) try! NSFileManager.defaultManager().moveItemAtURL(compactedURL, toURL: defaultURL) } func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { compactRealm()
Swift 3.0
func compactRealm() { let defaultURL = Realm.Configuration.defaultConfiguration.fileURL! let defaultParentURL = defaultURL.deletingLastPathComponent() let compactedURL = defaultParentURL.appendingPathComponent("default-compact.realm") autoreleasepool { let realm = try! Realm() try! realm.writeCopy(toFile: compactedURL) } try! FileManager.default.removeItem(at: defaultURL) try! FileManager.default.moveItem(at: compactedURL, to: defaultURL) } func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { compactRealm()
source share