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

Question about using Realm Swift and Realm Objective-C in the same app #8749

Open
Anhelme opened this issue Feb 27, 2025 · 1 comment
Open

Comments

@Anhelme
Copy link

Anhelme commented Feb 27, 2025

Context

We are currently using Realm Objective-C in our production app and considering migration options. Now our app mostly written on Swift, but we still use legacy Realm in Swift code too.
We found this discussion mentioning that Realm Swift and Realm Objective-C APIs are not interoperable

Questions

  1. Mixed Usage

    • Is it technically possible to use both Realm Swift and Realm Objective-C in the same app?
    • What kind of issues might we encounter if we try to do this?
    • Are there any known crash cases or data corruption risks?
  2. Migration Path

    • How complex would it be to migrate existing Realm Objective-C tables to Realm Swift?
    • Is there any migration tooling or recommended approach?
    • Would this migration be easier compared to migrating to a completely different database?
  3. Documentation

    • We noticed that the warning about mixing APIs is not present in current documentation
    • Could you clarify the current official stance on this?
@tgoyne
Copy link
Member

tgoyne commented Feb 27, 2025

Importing both the obj-c and Swift SDKs at once is perfectly fine and is in fact the intended way to use Realm in a mixed-language project now. RLMSupport.swift is now part of RealmSwift, along with some other helpers for using the obj-c API from Swift that couldn't be implemented in that way.

Interacting with Realm from Objective-C requires defining your models in objc, and using RLMObject subclasses requires using mostly the objc API. Mixing them doesn't work in that most things just won't compile due to there being different types involved, but there's otherwise no problems. As long as you don't work around problems with unsafeBitcast() or something there aren't really surprise problems where something will compile but not actually work.

Migrating to the Swift API will require touching nearly every line of code which touches Realm, but it should be exclusively very shallow changes. Everything is spelled slightly differently to align with Swift conventions, but there's no significant structural differences other than that the Swift API doesn't have the query functions which use the default Realm rather than take a Realm parameter.

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