You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Widget目标启动时Realm崩溃问题(Realm文件存于App Group)

Realm跨进程(主应用+Widget)共享数据崩溃问题排查求助

为实现主应用与Widget共享数据,我将Realm文件放置在App Group目录中,核心配置代码如下:

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)

其中Widget目标通过设置config.objectTypes降低内存占用。目前Xcode Organizer显示try! Realm(configuration: config)这一行频繁崩溃,崩溃仅发生在Widget触发且应用处于后台的场景,仅TestFlight用户反馈启动时收到崩溃通知。


UPDATE 1:Crashlytics捕获的两类错误

接入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

UPDATE 2:调整文件保护级别后的新问题

按照Realm官方指南降低Realm文件所在文件夹的文件保护级别,代码如下:

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

上述两类错误解决后,出现新的问题:

  • 数据库内存缓冲错误:
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)}
  • 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.

由于崩溃仅发生在应用后台,调试难度大,特寻求可行的解决方案,如需补充信息请告知。

内容的提问来源于stack exchange,提问作者Meroelyth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 23:03:10