我从 Realm 0.9x 和 2.0.x 更新了我的应用 现在我在 Crashlytics 上收到了来自 8、9 和 10 台 iOS 设备的大量关于致命异常的错误报告:
Fatal Exception: RLMException
Realm must not be nil
堆栈跟踪看起来像这样:
RLMObjectStore.mm line 81 RLMGetObjects
RLMObject.mm line 154 +[RLMObject objectsInRealm:withPredicate:]
RLMObject.mm line 146 +[RLMObject objectsInRealm:where:args:]
RLMObject.mm line 140 +[RLMObject objectsInRealm:where:]
Sessions.m line 88 +[Sessions resultWithSessionsWhere:]
Sessions.m line 128 +[Sessions activeSession]
Sessions.m line 102 +[Sessions appLaunched]
AppDelegate.m line 60 -[AppDelegate application:didFinishLaunchingWithOptions:]
我无法在调试环境中重现错误。它触动了大约 30-40% 的用户(根本没有),而且我似乎只触及了老用户。
此方法中没有 Realm 对象:
/**
* Create and returns Realm configuration for sessions database
*/
+(nonnull RLMRealmConfiguration*)realmConfigurationForSessions
{
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// Use the default directory, but replace the filename with the 'sessions.realm'
config.fileURL = [NSURL
URLWithString:[[[config.fileURL.absoluteString stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"sessions"]
stringByAppendingPathExtension:@"realm"]];
return config;
}
+(nullable RLMRealm*)realmForConfiguration:(nullable RLMRealmConfiguration*)config
{
if (!config) {
return nil;
}
NSError *error;
// Open the Realm with the configuration
RLMRealm *realm = [RLMRealm realmWithConfiguration:config
error:&error];
if (error) {
DLog(@"error: %@", error.localizedDescription);
}
return realm;
}
问题:1)这个奇怪问题的原因是什么? 2) 在这种情况下如何正确处理错误和 nil Realm 值?
最佳答案
那个异常occurs in a pretty straightforward case .发生错误,导致 RLMRealm
实例没有在应该创建的时候创建。
根据您提供的信息,您构建 fileURL
属性的方式可能不会在所有情况下都成功。
我的建议:
- 使用
[NSURL fileURLWithPath:]
而不是[NSURL URLWithString:]
。与 Web URL 相比,该优化针对文件路径进行了更好的优化。 - 使用
config.fileURL.path
而不是config.fileURL.absoluteString
。更多信息,为什么,check this SO answer . - 为了保持一致性,使用
[NSURL URLByAppendingPathComponent:]
而不是[NSString stringByAppendingPathComponent:]
。
希望对您有所帮助!如果您需要进一步说明,请告诉我!
关于ios - Realm 不能为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258419/