Search code examples
iosswiftrealmswift5ios14

Why can I no longer open my realm after upgrading my realm version


Related Issue on Realm Git

For a very long time I've been using Realm 3.17.3 in my swift app.

Let's call that App Version 1.

A short time ago I updated my podfile to use Realm 10.7.2.

Let's call that App version 2.

After doing this and when testing on the simulator my app sometimes crashes when trying to open the default realm file.

The crash usually occurs when I am running App Version 2, checkkout App Version 1, then do a pod install and run the app.

 self._realm = try! Realm(configuration: configuration) // crash

The error given by realm is:

NSError domain: "io.realm" - code: 2

"NSFilePath" : "../Library/Developer/CoreSimulator/Devices/15EF6132-9EA7-4946-81E4-78B48B8AC5E1/data/Containers/Data/Application/2BEC8269-1CD1-4D5D-A25F-A166D0AD5E73/Documents/default.realm"  

"Underlying" : "Invalid top array (ref: 53728, size: 11) Path: ../Library/Developer/CoreSimulator/Devices/15EF6132-9EA7-4946-81E4-78B48B8AC5E1/data/Containers/Data/Application/2BEC8269-1CD1-4D5D-A25F-A166D0AD5E73/Documents/default.realm"   

"NSLocalizedDescription" : "Unable to open a realm at path \'../Library/Developer/CoreSimulator/Devices/15EF6132-9EA7-4946-81E4-78B48B8AC5E1/data/Containers/Data/Application/2BEC8269-1CD1-4D5D-A25F-A166D0AD5E73/Documents/default.realm\': Invalid top array (ref: 53728, size: 11) Path:."  

My best guess is that the crash is because a newer version of realm is being replaced by an older version of realm.

I realise that this is not likely to be a real world scenario but I'm concerned that when I do release my "App version 2" that I'm going to have a LOT of crashes.

My specific questions are:

  1. Why is this crash happening?

  2. What can I do to prevent this crash?

  3. If I were to put my realm creation in a do catch, how could I recover from this error in the catch?


Solution

  • TL;DR

    In a nutshell, the issue is that Realm made significant changes to the underlying data structure from older versions to newer versions. While it does not require a migration* (the objects schema doesn't change) it does require that the SDK is updated at the same time the backing data is. This also requires Realm Studio to be updated accordingly.

    MORE INFO

    Here's where the issue is

    I've been using Realm 3.17.3 in my swift app.

    and then

    updated my podfile to use Realm 10.7.2.

    From the Realm 10.0 Version Docs

    NOTE: This version upgrades the Realm file format version to add support for new data types. Realm files opened will be automatically upgraded and cannot be read by versions older than v10.0.0.

    What that's saying is the data that backs your existing Realm objects is totally changed. So, for example, opening your Realm file with Realm Studio (10+) will update all of your data (it does show a message) and then your existing app will no longer be able to read it and will crash with this error

    Invalid top array (ref: 53728, size: 11) Path:

    Also note that if you do the inverse, where the SDK is updated and you try to open Realm with Realm Studio and get that error, you're using an older version of Realm Studio. You will need to update to Realm Studio 10.0 or higher.

    Note that by updating to 10.0 or higher, that version can no longer open the legacy Realm Cloud or Realm Object Server. You need to keep version 5.x around for that.

    *If you're using Realm Sync/MongoDB Realm Sync, there are significant changes from prior versions. Please review the MongoDB Realm Sync docs.