realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.32k stars 2.15k forks source link

[Xcode 16] Assigning `objectTypes` to Realm.Configuration fails to cast RealmSwift.Object.Type to RLMObjectBase #8677

Closed KillianSmith0 closed 2 months ago

KillianSmith0 commented 2 months ago

How frequently does the bug occur?

Always

Description

I am running Xcode 16 beta 6, and have not been able to get the latest release to compile with this Xcode version. As a result I am building the package from source through SPM

The problem

We are providing a list of objectTypes to the Realm.Configuration initializer

All these objects conform to RealmSwift.Object, even while outputting in the terminal po (BodyParts.self is RLMObjectBase.Type), the result is true

However, the compiler crashes when the objectTypes are accessed with the following error

Could not cast value of type 'C.RealmSwiftObject.Type' (0x11803da50) to 'C.RLMObjectBase.Type' (0x11803d558).

I have also tried using defaultConfig without providing objectTypes explicitly, but the SDK doesn't automatically pick up the objects as mentioned here.

Stacktrace & log output

Could not cast value of type '__C.RealmSwiftObject.Type' (0x11803da50) to '__C.RLMObjectBase.Type' (0x11803d558).

Can you reproduce the bug?

Always

Reproduction Steps

  1. Singleton to manage realm
  2. Initialize Realm.Configuration in init of singleton
  3. Assign to realm
  4. Run app
  5. Observe crash
private lazy var mainThreadRealm: Realm = {
    do {
        FFAssert(Thread.isMainThread)
        return try Realm(configuration: realmConfig)
    } catch let error {
        FFAssertError(error, "ERROR: Failed to access realm.")
        fatalError(error.localizedDescription)
    }
}()

/// The main realm.
var realm: Realm {
    do {
        if Thread.isMainThread {
            return mainThreadRealm
        } else {
            return try Realm(configuration: realmConfig, queue: bgQueue)
        }
    } catch let error {
        FFAssertError(error, "ERROR: Failed to access realm.")
        fatalError(error.localizedDescription)
    }
}

// list shortened for example
private lazy var objectTypes = [
    BodyParts.self,
    Equipment.self,
    UserDetails.self
]

init() {
    // .. 
    var defaultConfig = Realm.Configuration.defaultConfiguration
    defaultConfig.fileURL = realmUrl
    defaultConfig.schemaVersion = DataManager.schemaVersion
    defaultConfig.migrationBlock = migrationFunction
    defaultConfig.shouldCompactOnLaunch = compactorLogic
    defaultConfig.objectTypes = self.objectTypes
    self.realmConfig = defaultConfig

    // or even as
    realmConfig = Realm.Configuration(fileURL: realmUrl,
                                      schemaVersion: DataManager.schemaVersion,
                                      migrationBlock: migrationFunction,
                                      shouldCompactOnLaunch: compactorLogic,
                                      objectTypes: objectTypes)
}

Version

10.53.1

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 18

Build environment

ProductName: macOS ProductVersion: 15.0 BuildVersion: 24A5327a

/Applications/Xcode-beta.app/Contents/Developer Xcode 16.0 (beta 6) Build version 16A5230g

pod not found (not in use here)

/bin/bash GNU bash, version 3.2.57(1)-release (arm64-apple-darwin24)

carthage not found (not in use here)

/usr/bin/git git version 2.39.5 (Apple Git-154)

sync-by-unito[bot] commented 2 months ago

➤ PM Bot commented:

Jira ticket: RCOCOA-2426

tgoyne commented 2 months ago

This generally means that you've managed to link two different copies of Realm into your app, which results in two different RealmSwiftObject classes which aren't compatible with each other.

KillianSmith0 commented 2 months ago

Thank you for the response!

That is interesting, considering its fine in Xcode 15.x We are leveraging tuist so maybe its an Xcode 16 issue with their tooling 🤔

I'll try look into it further, thank you

KillianSmith0 commented 2 months ago

This seems to be a non-issue on Xcode 16 RC1. Will close, thanks for support!