Crashing issue with Realm when running app from widget target and realm file is placed in the app group

582 Views Asked by At

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:

  1. 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)}
  1. 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!

1

There are 1 best solutions below

7
Jay On

We've run into this many times. The error

Invalid top array

is caused by a version mis-match in your Realm file(s).

What that means specifically is that Realm has an underlying file format version and the SDK you're using cannot read that version of the file.

The fix is to update the SDK to a version that can read the file - that will in turn upgrade the file itself.

Without knowing which specific version are installed, I can't say what's needed but often times, ensuring the lastest SDK is installed and then opening the file with Realm Studio will often force an update and 'get them on the same page"

Realm Swift Releases

and

Realm Studio Releases

Be sure to read the release notes in case there any code changes.

For the other error

Unable to open a realm at path

It's possible your dealing with a File Protection issue and this is addressed in the documentation - here is is for reference

Use Realm When the Device Is Locked