ios - Show alert after app start -


i have problem showing alert after application start. using notification center observe notification error , find current visible view controller show alert on it. work when handle error after start app, if want show error after start app, not happen. code app delegate:

func listenforrealmerrornotification() {     notificationcenter.default.addobserver(forname: mydatamodeldidfailnotification, object: nil, queue: operationqueue.main, using: { notification in         let alert = uialertcontroller(title: nslocalizedstring("internal alert", comment: "internal error header"),                                       message: nslocalizedstring("there error while working whith data", comment: "internal error description") + "\n\n" + nslocalizedstring("press ok terminate app. sorry inconvenience", comment: "internal error excuses"),                                       preferredstyle: .alert)         let action = uialertaction(title: nslocalizedstring("ok", comment: "agree button on internal error"), style: .default, handler: {_ in             let exeption = nsexception(name: nsexceptionname.internalinconsistencyexception, reason: "realm error", userinfo: nil)             exeption.raise()         })         alert.addaction(action)         print("***observe error")         self.viewcontrollerforshowingalert().present(alert, animated: true, completion: nil)     }) } func viewcontrollerforshowingalert() -> uiviewcontroller {     let rootviewcontroller = self.window!.rootviewcontroller!     return topviewcontroller(from: rootviewcontroller) }  func topviewcontroller(from controller: uiviewcontroller) -> uiviewcontroller {     if controller uinavigationcontroller {         return topviewcontroller(from: (controller as! uinavigationcontroller).visibleviewcontroller!)     }     if controller uitabbarcontroller {         return topviewcontroller(from:(controller as! uitabbarcontroller).selectedviewcontroller!)     }     if let presentedviewcontroller = controller.presentedviewcontroller {         return topviewcontroller(from:presentedviewcontroller)     }      return controller } 

and code post notification:

func fatalrealmerror(_ error: error) { print("***fatal error database: \(error)") crashlytics.sharedinstance().recorderror(error) notificationcenter.default.post(name: mydatamodeldidfailnotification, object: nil) } 

update: initiating data source in delegate:

    func initialdatasource() {     {         datasource = try userdatasource()     }     catch let error nserror {         fatalrealmerror(error)     } } 

and here have set observer:

func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool {     customizeappearance()      listenforrealmerrornotification()     initialdatasource()     let rootviewcontroller = window?.rootviewcontroller as! uinavigationcontroller     let rootcontentcontroller = rootviewcontroller.viewcontrollers[0] as! yourfoodviewcontroller     rootcontentcontroller.datasource = datasource      fabric.with([crashlytics.self])     return true } 


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -