shouldCompactOnLaunch плюс определенный блок миграции. Невозможно перенести уже открытые области. - PullRequest
0 голосов
/ 07 мая 2019

Я пытаюсь сжать размер моей базы данных. У меня есть следующий конфиг в моем didFinishLaunchingWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    var config = Realm.Configuration(shouldCompactOnLaunch: { totalBytes, usedBytes in
            // totalBytes refers to the size of the file on disk in bytes (data + free space)
            // usedBytes refers to the number of bytes used by data in the file

            // Compact if the file is over 100MB in size and less than 50% 'used'
            let oneHundredMB = 100 * 1024 * 1024
            return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
        })

        do {
            // Realm is compacted on the first open if the configuration block conditions were met.
            _ = try Realm(configuration: config)
        } catch let err as NSError {
            // handle error compacting or opening Realm
            print("%%%%%%%%%%%%% in the catch \(err.localizedDescription)   !!!! %%%%%%%%%%%%%%")
        }

        config = checkForMigration(currently: config);
        // other stuff
        // calling methods which clear out some base64strings to url paths in some realm tables
        return true
   }

Где checkForMigration выглядит так:

func checkForMigration(currently config: Realm.Configuration) -> Realm.Configuration
    {
        var configu = config;
        var needsMigration: Bool = false;
        if util_Migration.schemaVersion != configu.schemaVersion
        {
            configu.schemaVersion = util_Migration.schemaVersion
            configu.migrationBlock = util_Migration.migration
            needsMigration = true;
        }

        Realm.Configuration.defaultConfiguration = configu

        if needsMigration {

            do { try Realm.performMigration()}
            catch let err as NSError
            {
                print("Realm Migration Error::: \(err.localizedDescription)");
            }
        }
        return configu;
    }

1. Как изменить это, чтобы настроить одновременно блок миграции и сжатие при запуске?

В настоящее время мое приложение работает при обновлении с предыдущей версии, но не при установке свежей. Я получаю эту ошибку при установке свежих:

Cannot migrate Realms that are already open

Мой метод миграции всегда работал раньше, когда я только что передал config = Realm.Configuration ()

2. Что положить в улов? Есть ли что-то, что может пойти не так, как надо, чтобы разобраться в этом улове?

3. Есть ли причина не оптимизировать по более строгим правилам? например если используется менее 80?

...