Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Sendable' class '...' cannot inherit from another class other than 'NSObject' for Realm Object Model in Swift6 #8744

Open
qihuijia opened this issue Feb 13, 2025 · 2 comments

Comments

@qihuijia
Copy link

How frequently does the bug occur?

Always

Description

I just create a Realm Object Model in package like this:

final class ItemGroup: Object, ObjectKeyIdentifiable, Sendable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var ownerId = ""
}

In Swift6 mode, I must make the object thread safe, so make it conform to Sendable.

and then there are 2 errors:

'Sendable' class 'ItemGroup' cannot inherit from another class other than 'NSObject'

Stored property '__id' of 'Sendable'-conforming class 'ItemGroup' is mutable

The package settings is like this:

// swift-tools-version: 6.0

import PackageDescription

let package = Package(
    name: "XXX",
    platforms: [
        .iOS(.v18)
    ],
    products: [
        .library(
            name: "XXX",
            targets: ["XXX"]
        )
    ],
    dependencies: [
        .package(url: "https://github.com/realm/realm-swift", from: "20.0.0")
    ],
    targets: [
        .target(
            name: "XXX",
            dependencies: [
                .product(name: "RealmSwift", package: "realm-swift")
            ],
            swiftSettings: [
                .swiftLanguageMode(.v6)
            ]
        ),
        .testTarget(
            name: "XXXTests",
            dependencies: ["XXX"]
        )
    ]
)

Stacktrace & log output

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

20.0.1

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 18.0

Build environment

Xcode version: 16.1

@tgoyne
Copy link
Member

tgoyne commented Feb 13, 2025

Realm objects are confined to an isolation context (a thread, queue, or actor) and cannot be made Sendable.

@qihuijia
Copy link
Author

@tgoyne OK. Thanks.

But even I use actor in Swift6, it has error.

actor RealmActor {
    // An implicitly-unwrapped optional is used here to let us pass `self` to
    // `Realm(actor:)` within `init`
    var realm: Realm!
    init() async throws {
        realm = try await Realm(actor: self)
    }
}

the error is Non-sendable type 'Realm' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants