【发布时间】:2017-07-04 19:18:00
【问题描述】:
我不完全确定从哪里开始。我在 Swift for iOS 10.3 中有一个使用 Crashlytics 和 Realm 的 iOS 应用程序,一天几次,当我启动我的应用程序时,它只是坐在启动屏幕上,然后立即关闭。发生这种情况时(通过 crashlytics 或在设备上),我没有收到任何日志,而解决问题的唯一方法是重新启动手机、重新安装应用程序,或者几个小时后重试。我不知道如何调试这个问题。
我的应用启动功能如下:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Fabric.with([Crashlytics.self])
// Override point for customization after application launch.
self.createDirectories()
var performShortcutDelegate = true
let dir: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.BackloggerSharing")!
let realmPath = dir.appendingPathComponent("db.realm")
let config = Realm.Configuration(fileURL: realmPath, schemaVersion: 1, migrationBlock: {
migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
// auto migrate
}
})
Realm.Configuration.defaultConfiguration = config
self.compactRealm(at: realmPath)
(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])).tintColor = Util.appColor
UISlider.appearance().tintColor = Util.appColor
self.window?.tintColor = Util.appColor
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
self.shortcutItem = shortcutItem
performShortcutDelegate = false
}
return performShortcutDelegate
}
func createDirectories() {
let playlistsFolder = Util.getPlaylistImagesDirectory()
if !FileManager.default.fileExists(atPath: playlistsFolder.absoluteString) {
do {
try FileManager.default.createDirectory(at: playlistsFolder, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog(error.localizedDescription)
}
}
}
func compactRealm(at realmPath: URL) {
let defaultParentURL = realmPath.deletingLastPathComponent()
let compactedURL = defaultParentURL.appendingPathComponent("default-compact.realm")
autoreleasepool {
let realm = try? Realm()
try! realm?.writeCopy(toFile: compactedURL)
}
try! FileManager.default.removeItem(at: realmPath)
try! FileManager.default.moveItem(at: compactedURL, to: realmPath)
}
任何关于此事的帮助将不胜感激!
【问题讨论】:
-
您是否尝试在 application:didFinishLaunchingWithOptions: 中放置断点?是否进入这个方法?
-
如果我从 xcode 重新启动应用程序,它工作正常。只是如果我已经使用我的应用程序一段时间了,无论是从 xcode 还是从 testflight,都会发生这种情况。在附加调试器的情况下,我无法捕捉到这种情况。
-
AppDelegate 中是否重新定义了 +initialize 方法?
-
另外,如果您从该方法返回 FALSE,应用程序将无法启动
标签: ios swift realm crashlytics