Good day!

I have already killed a lot of time, but nothing helps, but the problem remains. The bottom line is that I use Firebase Database Realtime and there is a global constant

ref = Database.database (). reference ()

The first launch of the application works fine, but when it is restarted, it is padata with an error:

2017-10-24 21:27:39.720991+0300 app[3027:89941] [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40004000 started 2017-10-24 21:27:39.721153+0300 app[3027:89941] [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see "GOOGLE URL") 2017-10-24 21:27:39.824589+0300 app[3027:89762] *** Terminating app due to uncaught exception 'RepoExists', reason: 'createRepo called for Repo that already exists.' *** First throw call stack: ( 0 CoreFoundation 0x00000001125be1cb __exceptionPreprocess + 171 1 libobjc.A.dylib 0x0000000111adaf41 objc_exception_throw + 48 2 CoreFoundation 0x0000000112632b95 +[NSException raise:format:] + 197 3 app 0x000000010eabaedf +[FRepoManager createRepo:config:database:] + 357 4 app 0x000000010eaa2531 -[FIRDatabase ensureRepo] + 151 5 app 0x000000010eaa1c29 -[FIRDatabase reference] + 31 6 app 0x000000010ea1bcf8 globalinit_33_CBBC6E383F6D8CD5592C2850E08E2698_func5 + 88 7 libdispatch.dylib 0x0000000115f5f43c _dispatch_client_callout + 8 8 libdispatch.dylib 0x0000000115f609c7 dispatch_once_f + 297 9 app 0x000000010ea1bd44 _T015app3refSo17DatabaseReferenceCfau + 36 10 app 0x000000010e9750f8 _T015app18LoadViewControllerC9updateTopyyF + 24 11 app 0x000000010e970fb5 _T015app18LoadViewControllerC07viewDidC0yyF + 5061 12 app 0x000000010e971384 _T015app18LoadViewControllerC07viewDidC0yyFTo + 36 13 UIKit 0x000000011339854d -[UIViewController loadViewIfRequired] + 1235 14 UIKit 0x000000011339899a -[UIViewController view] + 27 15 UIKit 0x0000000113266ae3 -[UIWindow addRootViewControllerViewIfPossible] + 122 16 UIKit 0x00000001132671eb -[UIWindow _setHidden:forced:] + 294 17 UIKit 0x000000011327a098 -[UIWindow makeKeyAndVisible] + 42 18 UIKit 0x00000001131ec521 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4711 19 UIKit 0x00000001131f1751 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1720 20 UIKit 0x00000001135b6e00 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 924 21 UIKit 0x0000000113989840 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153 22 UIKit 0x00000001135b69f9 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249 23 UIKit 0x00000001135b7257 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 668 24 UIKit 0x0000000113f2add9 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 262 25 UIKit 0x0000000113f2ac92 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444 26 UIKit 0x0000000113c0c87e __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 420 27 UIKit 0x0000000113e09e8e _performActionsWithDelayForTransitionContext + 100 28 UIKit 0x0000000113c0c67a -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231 29 UIKit 0x0000000113988e9d -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392 30 UIKit 0x00000001131effd2 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523 31 UIKit 0x00000001137c489b -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369 32 FrontBoardServices 0x000000011b4821b4 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338 33 FrontBoardServices 0x000000011b48aca9 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 235 34 libdispatch.dylib 0x0000000115f5f43c _dispatch_client_callout + 8 35 libdispatch.dylib 0x0000000115f64af4 _dispatch_block_invoke_direct + 592 36 FrontBoardServices 0x000000011b4b66ce __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24 37 FrontBoardServices 0x000000011b4b6384 -[FBSSerialQueue _performNext] + 464 38 FrontBoardServices 0x000000011b4b68f3 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 39 CoreFoundation 0x00000001125612b1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 40 CoreFoundation 0x0000000112600d31 __CFRunLoopDoSource0 + 81 41 CoreFoundation 0x0000000112545c19 __CFRunLoopDoSources0 + 185 42 CoreFoundation 0x00000001125451ff __CFRunLoopRun + 1279 43 CoreFoundation 0x0000000112544a89 CFRunLoopRunSpecific + 409 44 GraphicsServices 0x00000001199f99c6 GSEventRunModal + 62 45 UIKit 0x00000001131f323c UIApplicationMain + 159 46 app 0x000000010ea235b7 main + 55 47 libdyld.dylib 0x0000000115fdbd81 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 

How to be? Has anyone encountered a similar situation?

A method that uses this constant:

 func updateTop() { ref.child("Update").observeSingleEvent(of: .value, with: {(snapshot) in if let update = snapshot.children.allObjects as? [DataSnapshot] { for item in update { if String(describing: item.value!) == "true" { refTop.observeSingleEvent(of: .value, with: { (snapshot) in if let topItem = snapshot.children.allObjects as? [DataSnapshot] { for (index, item) in topItem.enumerated() { self.utils.loadHotEvent(topId: String(describing: item.childSnapshot(forPath: "id").value!), itemNum: index + 1) } updateTopGroup.notify(queue: DispatchQueue.main, execute: { ref.child("Update/Top").setValue("false") NotificationCenter.default.post(name: NSNotification.Name(rawValue: "segue"), object: nil) }) } }) } else { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "segue"), object: nil) } } } }) } 

    1 answer 1

    here is the same problem link , try putting your code in the main thread like this

     DispatchQueue.main.async { [unowned self] in self.ref = Database.database().reference() } 

    Here's another link . Database has a callbackQueue parameter which by default is main.

    • Strangely it turns out ... Does a constat in main thread need to be declared receive? - Egor
    • Apparently the Database.database (). reference () call itself should be in the main - Andrey Iskamov
    • But he is so main. Okay, how to get to the code, accomplish your goal. By the way, added a method that uses this constant - Egor