UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Xcode 14 warning --> " This method should not be called on the main thread as it may lead to UI unresponsiveness."

Forums > SwiftUI

@joxi  

Create the WKWebView in SwiftUI, It's working well below the Xcode version 14, but when I update my XCode to 14, receive two warning messages like below:

and the full code:

import SwiftUI
import WebKit

struct ContentView: View {

    var body: some View {
        WebView(url: URL(string: "https://bing.com")!)
    }
}

struct WebView: UIViewRepresentable {

    var url: URL

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

So, I want to know, it this a bug of Xcode 14? or have bug in my code? Thanks.

1      

I run into something similar when I try to make a MapKit Map in the new Xcode version, even though the warnings don't show up for Paul in the same project that he creates in one of the projects in the 100 days course. (The BucketList project)

Also, I've seen posts on various forums of people running into these same warnings when trying to use @Environment(\.dismiss) var dismiss to be able to dismiss their views shown in sheets in the latest Xcode version, while trying to figure out my problem.

I wasn't able to figure out for sure if there is some kind of bug with this, or if there is some better way that I should be using these things. But it's hard to know for sure when I'm still just learning.

1      

@joxi has been warned!

when I update my XCode to 14, I receive two warning messages.

This message should not be called on the main thread as it may lead to UI unresponsiveness.

Is this a bug in Xcode 14? or do I have bug in my code?

Let's ask some questions and investigate.

First, WHY? Why would some of this code cause the UI to be unresponsive?

Hypothesis: Sometimes visiting a web page in Safari takes a while to load. Maybe calling a web page via WKWebView may hang if the URL takes a while to resolve?

Test: Let's look at the documentation for clues.

See -> WKWebView documentation for load() method

The documentation says the load() method returns an optional WKNavigation object, which you're ignoring in your code.

What does the WKNavigation provide?

See -> WKNavigation Documentation

When you ask a web view to load content or navigate to a page,
the web view returns a WKNavigation object that identifies your request.
As the load operation progresses, the web view reports progress of that operation
to various methods of its navigation delegate, passing them the matching WKNavigation object.

This might be the clue!

If the WKNavigation object monitors the load operation progress, you might receive a code indicating unresponsive URL. In this case, your application might also suffer from UI updates.

As a work-around, you might want to first put up a "loading" graphic where your web view will be. Then monitor the progress of the loading web page via the WKNavigation object. When the web page is finally loaded, you remove the graphic, and display the web page.

I've not actually tested this with code, but this seems cromulent.

Keep Coding!

Please return here with sample code and let us know how you solved this.

1      

Additional Note:

Web sites like mockaroo.com and randomuser.me are great for simulating web services that provide thousand of rows of fake data (Department, Title, Company, Supervisor) and fake user data (First Name, Last Name, email, nationality, gender, home address).

However I don't see any tags allowing you to ask the service to delay the returned results.

Anyone know of such a service?

See -> Mockaroo Fake Data
See -> Fake User Data

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.