Saturday, February 10, 2024
HomeiOS Developmentios - Realm Swift Migration: Learn how to see if property/key exists?

ios – Realm Swift Migration: Learn how to see if property/key exists?


I’ve received a iOS primarily based realm app that has gone by plenty of schema modifications. For one schema model (model 36), I added a property named “activationStartGameTime” as a swift TimeInterval? on a category Interval. In a later schema model (model 38) I modified that property to an Int?. I’ve this in my migration:

if (oldSchemaVersion < 38) {
    migration.enumerateObjects(ofType: Interval.className()) { oldObject, newObject in
        if let startTimeInterval = oldObject?["activationStartGameTime"] as? TimeInterval {
            newObject!["activationStartGameTime"] = Int(startTimeInterval)
        } else {
            newObject!["activationStartGameTime"] = nil
        }
    ...

For customers who’ve obtained all variations of that app (particularly in the event that they get a model of the app that has realm schema 36 or 37, they’re tremendous since within the property activationStartGameTime was added. Nonetheless, when a consumer who’s utilizing a model of the app that has schema model < 36 tries to improve to the present app (schema model 38), they’re getting a crash as a result of the road

        if let startTimeInterval = oldObject?["activationStartGameTime"] as? TimeInterval {

is crashing. Plainly searching for a non-existant key through the use of oldObject?["some_non_existant_key"] at all times crashes with the error:

2024-02-08 20:46:05.398426-0800 FooApp... *** Terminating app attributable to uncaught exception 'RLMException', purpose: 'Invalid property identify 'activationStartGameTime' for sophistication 'Interval'.'

I can not seem to discover the magic incantation that can let me search for a property of an oldObject and never have it crash if the property doesn’t exist.

(I am on realm-swift 10.46.0, which is the most recent as of 2024_02_08)

Any concepts?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments