Firebase在发布时随机崩溃

Aak*_*ave 1 singleton ios firebase swift firebase-realtime-database

我在swift中有一个firebase应用程序.现在我的问题很容易解释,但调试真的很奇怪.

问题:每当我打开应用程序时,它都可以正常运行.是应该做的一切等等.但是当我在背景中离开应用程序一段时间并打开它时,我会崩溃.我必须重新打开它2次才能发生撞击或杀死它.

重新产生错误:第1步:打开应用程序(在那里做一些事情或者只是离开它)然后回到家里而不是杀死它,将它保留在后台

第2步:过一会儿打开应用程序.--->崩溃

步骤3:在一段时间之后在步骤2之后再次打开它或立即--->再次崩溃

第4步:第三次打开它.--->工作完全没问题.

注意:为了逃避崩溃我总是要杀死它.总的来说,这是一个非常糟糕

另外,我检查了我的plists.他们运作正常.然而,当它崩溃我得到这个错误:

2018-12-20 19:05:44.272413-0800 SpotMi[15557:862318] *** Terminating app due to uncaught exception 'FIRAppNotConfigured', reason: 'Failed to get default Firebase Database instance. Must call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift) before using Firebase Database.'
*** First throw call stack:
(0x1a1e2bea0 0x1a0ffda40 0x1a1d32674 0x1030c8d20 0x102f9cd24 0x102f992b0                     0x102f9926c 0x10f928dc8 0x10f92ae28 0x10ef95e18 0x102f9935c 0x102f49644     0x102f4a55c 0x102f4ad94 0x102e03b18 0x102d5cd4c 0x102f49260 0x102f493b0 0x102f4ac74 0x102f4b11c 0x102dbaf3c 0x102dbafd8 0x1cea6620c 0x1cea6663c 0x1cf0551e4 0x1cf051200 0x1cf0240a0 0x1cf02585c 0x1cf02b2a8 0x1ce8c7358 0x1ce8cffd8 0x1ce8c6fd4 0x1ce8c7974 0x1ce8c5a74 0x1ce8c5720 0x1ce8ca8e0 0x1ce8cb840 0x1ce8ca798 0x1ce8cf684 0x1cf0297a0 0x1cec12bac 0x1a48609d4 0x1a486b79c 0x1a486ae94 0x10f928dc8 0x10f92ca10 0x1a489fa9c 0x1a489f728 0x1a489fd44 0x1a1dbc1cc 0x1a1dbc14c 0x1a1dbba30 0x1a1db68fc 0x1a1db61cc 0x1a402d584 0x1cf02d054 0x102d5da9c 0x1a1876bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
Run Code Online (Sandbox Code Playgroud)

的appdelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    #if DEVELOPMENT
    print("Development Mode Started")
    let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
    guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!)
        else {
            fatalError("Couldn't load config file")
    }
    FirebaseApp.configure(options: fileopts)

    #else
    print("Production Mode Started")
    FirebaseApp.configure()
    #endif

    return true
}
Run Code Online (Sandbox Code Playgroud)

我尝试将此补丁放入init(),但随后通知停止工作.

我应该如何处理这个问题.我做错了什么?

如果需要更多信息,请与我们联系.

我的firebase单身如下:

class FBDataservice : NSObject {

static var ds = FBDataservice() //<-------- Always crahses on this line

let DB_URL: DatabaseReference = Database.database().reference()
let ST_URL: StorageReference = Storage.storage().reference()

private lazy var _REF_BASE = DB_URL
private lazy var _REF_POSTS = DB_URL.child("key1")
private lazy var _REF_USERS = DB_URL.child("key2")

private lazy var _ST_REF_PROFPOST = ST_URL.child("key3")

var REF_BASE: DatabaseReference! {
    return _REF_BASE
}

var REF_POSTS: DatabaseReference! {
    return _REF_POSTS
}

var REF_USERS: DatabaseReference! {
    return _REF_USERS
}

var REF_POST_USERIMG: StorageReference {
    return _ST_REF_USERPOST
}


}
Run Code Online (Sandbox Code Playgroud)

jms*_*jms 6

AppDelegate将初始化所有初始视图控制器(以及选项卡导航控制器中的所有主视图控制器),viewDidLoad()并在应用程序启动期间调用它们.这是设计的(在appDelegate的方法之前调用viewController的viewdidload)

问题是ds在init()或viewDidLoad()方法中访问您的变量.这导致在调用FirebaseApp.configure()和每个viewDidLoad()方法之间发生竞争条件.如果FirebaseApp.configure()在viewcontroller初始化之前完成,你就可以了.

要解决此问题,您需要接管故事板初始化给自己.这需要两个步骤:

  1. 在Xcode中选择您的项目 - >常规.向下滚动到主界面并将其删除
  2. 在AppDelegate applicationDidFinishLaunching方法中,初始化所需的故事板,如下所示:

    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        // configure Firebase
    
        window = UIWindow(frame: UIScreen.main.bounds)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        window?.rootViewController = storyboard.instantiateInitialViewController()
        self.window?.makeKeyAndVisible()        
    
        return true
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)