- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经发布了一个使用 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/
我在使用以下代码时遇到问题: function http_file_exists($url){ $f=fopen($url,"r"); if($f){ fclose($f); retu
我已经通过 Git 部署到 Azure 几个月了,没有出现重大问题,但现在我似乎遇到了一个无法克服的错误。 我创建了一个新的 Azure 网站,为正在开发的项目创建单独的预览链接。我在新站点上设置了
我已经通过flutter创建了一个App并完成了它,我想在flutter文档中阅读时进行部署。 我收到此错误: FAILURE: Build failed with an exception. * W
我在Windows 10中使用一些简单的Powershell代码遇到了这个奇怪的问题,我认为这可能是我做错了,但我不是Powershell的天才。 我有这个: $ix = [System.Net.Dn
我正在尝试使用 RapidJSON 解析从服务器接收到的数据。以下是收到的确切字符串: [ { "Node": "9478149a08f9", "Address": "172.17
我尝试为 ios 编译 OpenCV。我总是收到这些错误。我用不同版本的opencv试了一下,结果都是一样的。 我运行这个:python 平台/ios/build_framework.py ios_o
我在一台机器上做基本的发布/订阅,我的客户端是 StackExchange-Redis 的 C# 客户端,我在同一台机器上运行基于 Windows 的 Redis 服务器(服务器版本 2.8.4) 当
我有这段代码,但无法执行,请帮我解决这个问题 连接 connect_error) { die ("connection failed: " . $terhubung->connect_erro
我在 tomcat 上运行并由 maven 编译的 Web 应用程序给出了以下警告和错误。我可以在本地存储库中看到所有 JAR,但有人可以帮忙吗。 WARNING: Failed to scan JA
我正在 Windows 8 上使用 Android Studio 开发一个 android 应用程序,我正在使用一些 native 代码。突然间我无法编译我的 C 文件。当我运行 ndk-build
下面的代码对类和结构的成员进行序列化和反序列化。序列化工作正常,但我在尝试使用 oarch >> BOOST_SERIALIZATION_NVP(outObj); 反序列化时遇到了以下错误; 代码中是
如果我运行此命令“rspec ./spec/requests/api/v1/password_reset_request_spec.rb”,此文件中的所有测试都会通过。 但是,当我运行“rspec”时
我在尝试执行测试以使用 Protractor 上传文件时出错,我的代码是这个 it('it should be possible to upload a file', function() {
System.loadLibrary("nativefaceswap"); 当我运行我的应用程序时,我在 Android Studio 中发现了此类错误。在logcat中显示: java.lang.U
我希望有人能帮助我!使用任何方法或命令行的任何 SSL/HTTPS 调用均无效。 我在 Windows 10 中使用 Ubuntu Server 18.04 作为子系统。我的问题是昨天才开始出现的,因
通过删除这两个值将日期字段从 null=True 和 Blank=True 更改为 required 时,使用 db.alter 命令时遇到问题。 当以下行被注释掉时,迁移运行不会出现问题。
我第一次使用 Heroku 尝试创建应用程序(使用 SendGrid 的 Inbound Parse Webhook"和 Twilio SMS 通过电子邮件发送和接收 SMS 消息)。通过 Virtu
我正在将我的 swift 项目更新到 Xcode 7 上的 Swift 2.0。xcode 在构建项目时报告了以下错误: 命令/Applications/Xcode.app/Contents/Deve
在我的代码中,SSL 库函数 SSL_library_init() 没有按预期返回 1。我如何才能看到它返回了什么错误? 我在 SSL_library_init() 之后调用了 SSL_load_er
我正在尝试运行在以下链接中找到的答案: Asynchronously Load the Contents of a Div 但是当我这样做时,我会遇到我不太理解的错误。 我的代码: $(documen
我是一名优秀的程序员,十分优秀!