简体   繁体   中英

“How to retrieve an url from Firebase Database BEFORE load WebView in Swift?”

I am building an application with webView and Firebase as database. The url that should be used to load the webView should come from the database.

I have implemented both webView and Firebase correctly but the problem is that my webView is not loading with the url from the database but the url declared in the var declaration.

It seems that viewDidLoad call in priority the methods webView load before the closure inside Database Observe method

If I look at the console, I can clearly see that everything is working BUT the url that should be used to load the webview is retrieved from the database AFTER webview load...this should be done BEFORE given that I put the retrieve method BEFORE...

class ViewController:  UIViewController {

    var dbRef: DatabaseReference!
    var nextUrl = "https://www.google.co.uk/"

    func retrieveUrl () {
        dbRef = Database.database().reference().child("EXAMPLE")

        dbRef.observe(.value) { (snapshot) in
            let value = snapshot.value as! NSDictionary
            let url = value["url"]!
            self.nextUrl = (url as! String)
            print (nextUrl)
        }
        print ("function observe is called")
    }

    override func viewDidLoad () {
        super.viewDidLoad()
        retrieveUrl()
        print ("webview is about to load")
        let request = URLRequest (url: URL(string: nextUrl)!)
        self.webView.load(request)
        self.webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
    }
}

in the console I can see that "webview is about to load" is printed before nextUrl...which is very strange to me

You should load webView in callback of firebase observer.

Because you are loading webView right after firebase network call its like async work, so webView try to loads with nextUrl is empty or not, to prevent async like behavior you must wait for nextUrl or you should load webView in firebase callback Do it like following

class ViewController:  UIViewController {
    var dbRef: DatabaseReference!
    var nextUrl = "https://www.google.co.uk/"
    func retrieveUrl () {
        dbRef = Database.database().reference().child("EXAMPLE")
        dbRef.observe(.value) {
            (snapshot) in
            let value = snapshot.value as! NSDictionary
            let url = value["url"]!
            self.nextUrl = (url as! String)
            print (nextUrl)
            print ("webview is about to load")
            let request = URLRequest (url: URL(string: nextUrl)!)
            self.webView.load(request)
            self.webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
        }
        print ("function observe is called")
    }

    override func viewDidLoad () {
        super.viewDidLoad()
        retrieveUrl()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM