【问题标题】:iOS app will not launch until restart phoneiOS 应用程序在重启手机后才会启动
【发布时间】: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


【解决方案1】:

原来是我在今天的扩展中打开了一个领域,但没有关闭领域实例。当我加载我的应用程序时,我会尝试打开另一个领域(现在在不受支持的第二个进程中),并执行迁移和压缩。我已更新 Today Extension 以在不需要时关闭领域实例。

此调试是根据我的应用程序未保存日志确定的,因为我达到了应用程序允许的最大值 (25)。我清理了日志,找到了它们,象征了它们,发现Realm在打开时很挣扎。

【讨论】:

    【解决方案2】:

    如果您使用 Realm,并且您在数据库中进行了更改,您应该重新安装应用程序,否则它会在启动屏幕上崩溃, 如果上述不是问题,那么您的应用可能使用了最大内存,或者您的某些代码会导致内存泄漏。

    【讨论】:

    • 我不做任何改变。该应用程序将正常工作,然后有一次它会停止工作。从 xcode 重新启动工作正常,但如果我只是从跳板点击应用程序,它有时会挂起和崩溃,无论我尝试多少次,我都无法打开它。我想杀死一个应用程序也会释放它的内存,但我不确定是否是这种情况。
    • 当您从跳板点击应用时,您的应用是处于后台状态还是不存在于后台
    • 可能在 didFinishLaunchingWithOptions 中返回 false 会导致问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多