realm / realm-swift

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

SwiftUI Previews + Device Sync: Make It Less Painful? #8528

Closed bdkjones closed 5 months ago

bdkjones commented 5 months ago

Problem

I have an app that uses Realm and Atlas Device Sync. If I add a SwiftUI view and allow Xcode to show me a preview of it, Xcode has to invisibly build and run the entire app (regardless of whether anything in the SwiftUI view depends on anything else in the app).

The trouble is that the next time I hit the "play" button in Xcode to actually build and run the app, it immediately crashes with a "multiple sync clients connected" exception. Because the SwiftUI preview version is still running.

Closing the preview canvas or switching away from it by selecting a different file does not solve the issue. The only reliable solution is to quit and relaunch Xcode. This is painful.

Apple is unlikely to ever solve this. They don't care about people using Realm. Is there anything that can be done from Realm's side to make SwiftUI previews more friendly?

Solution

The ideal solution is that a true build-and-run of the app (from the user clicking the play button in Xcode) "overrides" any other running version of the binary from the SwiftUI preview. Functionally, I imagine that's difficult.

Alternatives

No response

How important is this improvement for you?

Would be a major improvement

Feature would mainly be used with

Atlas Device Sync

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

➤ PM Bot commented:

Jira ticket: RCOCOA-2321

nirinchev commented 5 months ago

We have a project in progress to allow multiple processes to access the same Realm file, that should make this go away. In the meantime, what you could do is change the path where the Realm file is stored based on the value of the XCODE_RUNNING_FOR_PREVIEWS environment variable. You could modify the path of the Realm file by setting Configuration.fileUrl.