Find schemaVersion of realm in swift 4.0

2019-08-31 07:04发布

问题:

I am relatively new in this field, and really stuck here as how to get the current schemaVersion of realm.

I tried Below code to check my schemaVersion

let configCheck = Realm.Configuration();
    let configCheck2 = Realm.Configuration.defaultConfiguration;
    let schemaVersion = configCheck.schemaVersion
    print("Schema version \(schemaVersion) and configCheck2 \(configCheck2.schemaVersion)")

but Output was coming zero always

Schema version 0 and configCheck2 0

But I have already changed my schemaVersion when Migrated, by This

        let config = Realm.Configuration(
        schemaVersion: 2,
        migrationBlock: { migration, oldSchemaVersion in

            if (oldSchemaVersion < 2) {
                // Nothing to do!

            }
    })
    Realm.Configuration.defaultConfiguration = config

I found This Answer

schemaVersionAtPath(_:encryptionKey:error:).

I am not understanding the parameters. Any example with prameters would be really helpful.

回答1:

I am answering to my own question

let configCheck = Realm.Configuration();
    do {
         let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
        print("schema version \(fileUrlIs)")
    } catch  {
        print(error)
    }

and the output was

schema version 2