NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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.

   

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.

   

@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.

   

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

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.