I'm facing an issue with my app where I receive a lot of crashes at the line try Realm(configuration: config).
In my code, I'm using the following approach to read the Realm database from both the main app and the widgets:
var config = Realm.Configuration(schemaVersion: 1)
let container = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.me.myapp")
let newRealmURL = container?.appendingPathComponent("database.realm")
config.fileURL = newRealmURL!
#if TARGET_IS_WIDGET
config.objectTypes = [EOperationParams.self, ESocketMessage.self, EActivity.self]
#endif
try! Realm(configuration: config)
To ensure data consistency, I have placed the realm file in the app group so that both the app and widgets can access the same data. However, when the target is a widget, I'm adding config.objectTypes with all the objects in my database. This approach has helped reduce memory usage for reading the realm database within the widgets.
While this code usually works fine, I am seeing numerous crashes at the line try Realm(configuration: config) in the "Crashes" section of Xcode's Organizer. Surprisingly, I haven't received any user feedback regarding this issue except from TestFlight users who encounter a crash notification upon launching the app. This crash appears to occur when the app is in the background and triggered by the widget. I am only experiencing this crash when running the widget target. Can someone please assist me in resolving this issue?
UPDATE 1
I added crashlytics and I found a lot of crashes like this:
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=2 "Unable to open a realm at path '': Invalid top array (top_ref, [0], [1]): 17776, 399336, 431888 Exception backtrace: 0...
Or like this:
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=2 "Unable to open a realm at path '/private/var/mobile/Containers/Shared/AppGroup/335A9037-C38C-5D95-AC07-69D3B23BB4B2/database.realm.lock': open() failed: Operation not permitted Path: /private/var/mobile/Containers/Shared/AppGroup/335A9037-C38C-5D95-AC07-69D3B23BB4B2/database.realm.lock Exception backtrace: 0
UPDATE 2
I've made some progress with my issue and have followed the official guide from Realm to downgrade the file protection of the folder containing the Realm files. I've added this code as per their suggestion:
try! FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication],
ofItemAtPath: folderPath)
The previous errors are now resolved, but I'm encountering two new errors:
- I'm getting a fatal error:
try!expression unexpectedly raised an error:
Error Domain=io.realm Code=20 "Failed to memory buffer:Invalid top array size (ref: 20160, array size: 3236197) file size: 12824, read lock size: some(32768), read lock version: some(2)" UserInfo={Error Code=20, NSFilePath=, Error Name=InvalidDatabase, NSLocalizedDescription=Failed to memory buffer:Invalid top array size (ref: 20160, array size: 3236197) file size: 12824, read lock size: some(32768), read lock version: some(2)}
- I've managed to get it sent only from the macCatalyst version, and it says:
Realm file '/.../group.com.me.myapp/database.realm' is currently open in another process which cannot share access with this process. This could either be due to the existing process being a different architecture or due to the existing process using an incompatible version of Realm. If the other process is Realm Studio, you may need to update it (or update Realm if your Studio version is too new), and if using an iOS simulator, make sure that you are using a 64-bit simulator. Underlying problem: Architecture mismatch: SharedInfo size is 1440 but should be 1456.
I would greatly appreciate any insights or suggestions to help resolve this issue. Since it only occurs when the app is in the background, it makes it particularly challenging to debug. If you need any additional information or code, please let me know.
Thank you in advance for your help!
try Realm(configuration: config)is not valid code and should be throwing a compiler error "Errors thrown from here are not handled"group.com.me.myapp"exactly match one of the strings in the app's App Groups Entitlement? Also, the question states "a lot of crashes" which is vague. One issue is that optionals (which could be nil) are not being handled. Thislet newRealmURL = container?and thisconfig.fileURL = newRealmURL!are asking for trouble as those optionals could be nil and unexpectedly crash your code. You should handle those as if they could be nilguard let container =... else { print("URL ERROR")... return }for example. Please do more troubleshooting to determine the nature of the crash.