Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 844 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Xcode UIView.init(frame:) must be used from main thread only

#1
I'm trying to render some views in background thread to not affect the main thread. That was never a problem before Xcode 9.

DispatchQueue.global(qos: .background).async {
let customView = UIView(frame: .zero)
DispatchQueue.main.async {
self.view.addSubview(customView)
}
}

> UIView.init(frame:) must be used from main thread only

This error occurs in the second line.

**Update**

The Apple `UIView` Documentation actually says in the *Threading Considerations* section:

> Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView class from code running in the main thread of your application. **The only time this may not be strictly necessary is when creating the view object itself, but all other manipulations should occur on the main thread.**
Reply

#2
For Objective C:


dispatch_async(dispatch_get_main_queue(), ^{

// UIView usage
self.view.userInteractionEnabled = YES;
});


Reply

#3
### Main Thread Entry

You can enter the main thread as follows

DispatchQueue.main.async {
// UIView usage
}
Reply

#4
Xcode 9 has a new runtime **[Main Thread Checker][1]** that detects call to UIKit from a background thread and generate warnings.

I know its meant to generate warnings and not crash the app, but you can try disabling Main Thread Checker for your test target.


[![enter image description here][2]][2]



I tried this code in a sample project, the debugger paused at the issue (as it is supposed to), but the app didn't crash.

override func viewDidLoad() {
super.viewDidLoad()

DispatchQueue.global().async {
let v = UIView(frame: .zero)
}
}


[1]:

[To see links please register here]

[2]:
Reply

#5
You can use this function

func downloadImage(urlstr: String, imageView: UIImageView) {
let url = URL(string: urlstr)!
let task = URLSession.shared.dataTask(with: url) { data, _, _ in
guard let data = data else { return }
DispatchQueue.main.async { // Make sure you're on the main thread here
imageview.image = UIImage(data: data)
}
}
task.resume()
}

How to use this function?

downloadImage(urlstr: "imageUrl", imageView: self.myImageView)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through