从小部件目标运行应用程序且领域文件放置在应用程序组中时,领域出现崩溃问题

问题描述 投票:0回答:1

我的应用程序遇到了一个问题,我在线路上遇到了很多崩溃问题

try Realm(configuration: config)

在我的代码中,我使用以下方法从主应用程序和小部件读取领域数据库:

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)

为了保证数据的一致性,我将realm文件放在了app组中,这样app和widget都可以访问相同的数据。但是,当目标是小部件时,我将

config.objectTypes
添加到数据库中的所有对象。这种方法有助于减少在小部件中读取领域数据库的内存使用量。

虽然这段代码通常工作正常,但我在 Xcode 组织器的“崩溃”部分的 try Realm(configuration: config) 行看到大量崩溃。令人惊讶的是,除了在启动应用程序时遇到崩溃通知的 TestFlight 用户之外,我没有收到任何有关此问题的用户反馈。当应用程序位于后台并由小部件触发时,似乎会发生此崩溃。我仅在运行小部件目标时遇到此崩溃。有人可以帮我解决这个问题吗?

更新1

我添加了 crashlytics,发现了很多这样的崩溃:

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...

或者像这样:

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

更新2

我的问题已经取得了一些进展,并按照 Realm 的官方指南降级了包含 Realm 文件的文件夹的文件保护。我按照他们的建议添加了这段代码:

try! FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication],
                                       ofItemAtPath: folderPath)

以前的错误现已解决,但我遇到了两个新错误:

  1. 我收到致命错误:
    try!
    表达式意外引发错误:
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. 我设法仅从 macCatalyst 版本发送它,它显示:
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.

我将非常感谢任何有助于解决此问题的见解或建议。由于它仅在应用程序处于后台时发生,因此调试起来特别困难。如果您需要任何其他信息或代码,请告诉我。

提前感谢您的帮助!

ios swift realm widgetkit ios-app-group
1个回答
0
投票

我们已经多次遇到过这种情况。错误

顶部数组无效

是由您的 Realm 文件中的版本不匹配引起的。

这具体意味着 Realm 有一个底层文件格式版本,而您使用的 SDK 无法读取该版本的文件。

修复方法是将 SDK 更新到可以读取该文件的版本 - 这将依次升级文件本身。

在不知道安装了哪个特定版本的情况下,我无法说出需要什么,但很多时候,确保安装了最新的 SDK,然后使用 Realm Studio 打开文件通常会强制更新并“让它们在同一页面上”

Realm Swift 发布

Realm Studio 发布

请务必阅读发行说明,以防代码发生任何更改。

对于其他错误

无法在路径上打开领域

您可能正在处理文件保护问题,这已在文档中解决 - 这里仅供参考

设备锁定时使用领域

© www.soinside.com 2019 - 2024. All rights reserved.