ios - 偶尔创建加密 Realm 对象失败的原因可能是什么?

标签 ios swift realm

我已经发布了一个使用 Realm 的应用程序,并且有一些崩溃日志显示有时无法使用导致 EXC_BREAKPOINT (SIGTRAP) 崩溃的配置创建 Realm 。 (几百个应用程序安装有 9 个崩溃文件,所以这不是经常发生的事情)

@objc class Database : NSObject
{    
    let configuration = Realm.Configuration(encryptionKey: Database.getKey() as Data)
    var transactionRealm:Realm? = nil

    override init()
    {
        let realm = try! Realm(configuration: configuration) // Crash here
        <snip>
    }

    // This getKey() method is taken from the Realm website
    class func getKey() -> NSData {
        let keychainIdentifier = "Realm.EncryptionKey.AppKey"
        let keychainIdentifierData = keychainIdentifier.data(using: String.Encoding.utf8, allowLossyConversion: false)!

        // First check in the keychain for an existing key
        var query: [NSString: AnyObject] = [
            kSecClass: kSecClassKey,
            kSecAttrApplicationTag: keychainIdentifierData as AnyObject,
            kSecAttrKeySizeInBits: 512 as AnyObject,
            kSecReturnData: true as AnyObject
        ]

        // To avoid Swift optimization bug, should use withUnsafeMutablePointer() function to retrieve the keychain item
        // See also: http://stackoverflow.com/questions/24145838/querying-ios-keychain-using-swift/27721328#27721328
        var dataTypeRef: AnyObject?
        var status = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0)) }
        if status == errSecSuccess {
            return dataTypeRef as! NSData
        }

        // No pre-existing key from this application, so generate a new one
        let keyData = NSMutableData(length: 64)!
        let result = SecRandomCopyBytes(kSecRandomDefault, 64, keyData.mutableBytes.bindMemory(to: UInt8.self, capacity: 64))
        assert(result == 0, "REALM - Failed to get random bytes")

        // Store the key in the keychain
        query = [
            kSecClass: kSecClassKey,
            kSecAttrApplicationTag: keychainIdentifierData as AnyObject,
            kSecAttrKeySizeInBits: 512 as AnyObject,
            kSecValueData: keyData,
            kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlock
        ]

        status = SecItemAdd(query as CFDictionary, nil)
        assert(status == errSecSuccess, "REALM - Failed to insert the new key in the keychain")

        return keyData
    }

这是崩溃文件的相关部分:

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000103c0f30c
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [0]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib              0x0000000103c0f30c 0x103ab4000 + 1422092
1   libswiftCore.dylib              0x0000000103c0f30c 0x103ab4000 + 1422092
2   libswiftCore.dylib              0x0000000103b13d2c 0x103ab4000 + 392492
3   libswiftCore.dylib              0x0000000103b13bf4 0x103ab4000 + 392180
4   My app                          0x000000010334ff14 _TFC14Caller_Name_ID8DatabasecfT_S0_ + 1648 (Database.swift:21)
5   My app                          0x0000000103330624 -[Model createDatabase] + 200 (Model.m:271)

Database.swift 文件的第 21 行是上面代码中 Init() 方法中指示的行。

如果 Realm 崩溃的原因是 getKey() 失败,那么为什么会失败?如果那不是原因,那么失败的其他原因是什么? 如果出现故障,代码可以做什么(例如重试创建 Realm 对象)?

最佳答案

初始化方法

我要先解决这个问题:在这种情况下,这不太可能与您的崩溃有任何关系,但是当您覆盖任何 init 方法时,您应该始终调用该 init 方法的 super 版本,除非没有可调用的父类(super class)或可用的父类(super class)实现。在 Objective-C 中,您将 [super init] 的结果分配给 self,但是,这种模式并未被 swift 采用,并且从 Apple 的文档中不清楚,当您从直接 NSObject 子类调用 super.init() 时会发生什么 swift 。这个时候我已经筋疲力尽了,以为我确实花了一些时间查看 apple/swift GitHub 存储库 Apple/Swift Github在其他地方,无法找到关于从 Swift 子类调用 NSObject init 的任何真正令人满意的信息。如果你真的想了解更多,而不是只相信我的话,我肯定会鼓励你继续搜索。在那之前,如果您从 Swift 子类调用 NSObject 的 init() ,它不太可能会导致问题,而且它也可能在某个时候拯救您的屁股! ;)

崩溃

If the reason for the Realm crash is because getKey() fails, then why would that be failing?

我不确定为什么 getKey() 可能会失败,但这不太可能是您崩溃的原因。我相信属性的默认值在调用 init() 内的代码时可用,在这种情况下,您的应用程序会在到达 init() 内的调用之前崩溃。明天我会对此做更多的研究。

If thats not the cause, that what are other reasons for the failure?

我查看了您在那里使用的 Realm API,目前尚不清楚为什么对 Realm(configuration:) 的调用失败,尽管很明显这是可能的,因为调用可能会抛出。但是,文档没有说明它会抛出的条件。

And is there anything the code can do (such as retry to create the Realm object) if there is a failure?

是的!幸运的是,“try”机制除了“try!”之外还有其他变体。实际上,在这种情况下,应用程序似乎崩溃的原因是您正在使用 try!在生产代码中,它只应在您知道调用极不可能失败的情况下使用,例如从应用程序包中检索应用程序附带的资源。根据 Apple 的文档:

Sometimes you know a throwing function or method won’t, in fact, throw an error at runtime. On those occasions, you can write try! before the expression to disable error propagation and wrap the call in a runtime assertion that no error will be thrown. If an error actually is thrown, you’ll get a runtime error. Swift Error Handling Documentation

在谈到影响用户体验时,我总是宁可谨慎行事,所以即使发现一次尝试,我也会感到非常惊讶!在我的任何代码中(甚至是 Swift Playground 玩具和实验)。

为了优雅地失败并重试或采取其他行动,您的代码需要使用 try?这会将抛出的错误转换为可选的,如:

if let realm = try? Realm(configuration: configuration) {
    // Do something with realm and ignore the error
}

或者您可以像这样使用完整的 try-catch 机制:

let realm: Realm? = nil
do {
   realm = try Realm(configuration: configuration)
   // do something with realm
} catch let e {
   realm = nil
   // do something with the error
   Swift.print(e)
}

或者:

do {
    let realm = try Realm(configuration: configuration)
    // do something with realm
} catch let e {
    // do something with the error
    Swift.print(e)
}  


所以这可能不是你永远不会让这个 Realm 调用失败的金票,但我希望它能为你的代码提供一些帮助,使你的代码更加健壮。对于任何错误,我深表歉意,我来晚了。祝你好运,干杯! :)

关于ios - 偶尔创建加密 Realm 对象失败的原因可能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246332/

相关文章:

realm - 在 swift 中查询嵌套可选 Realm 对象的最简单方法

ios - 如何使用 GET/repos/:owner/:repo/labels/:name 制作 url

asynchronous - 如何在 Playground 中运行异步回调

ios - 如何在 Xcode 的 UITests 下以通用方式访问后退栏按钮项目?

android - RealmResults 与 RealmList 之间的区别?

swift - 删除一对多关系

ios - 不使用 EAGLContext 的 OpenGL ES 示例

ios - 如何在 Crashlytics/Fabric 中更改 iOS 应用程序图标

ios - 网站在 iPhone 5 上的显示与 Chrome 的 iPhone 5 模拟器不同

ios - UiTableView 标题不会消失