dispatch-async

Can we update UI from background Thread?

≡放荡痞女 提交于 2019-12-02 05:32:13
Hello iOS experts just to clear my concept I have a bit confusion about UI updation from Main Thread. Apple requirements are that all UI related stuff should be carried out in main Thread.So to test: Case1: I dispatch a task to a global dispatch concurrent queue asynchronously . After some processing I update my UI stuff directly from the concurrent queue (background thread), working fine using the below code. dispatch_queue_t myGlobalQueue; myGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(myGlobalQueue, ^{ // Some processing // Update UI; }); Case2

Passing an argument to dispatch_async

时间秒杀一切 提交于 2019-12-02 04:15:34
I'm new to Swift and looking at how the dispatch_async function works. The API doc shows dispatch_async having two parameters. However, I'm able to pass in one argument and it's okay. dispatch_async(dispatch_get_main_queue()) { } How come I don't need to pass in two arguments? Thank you, API Doc: It is a trailing closure syntax func someFunctionThatTakesAClosure(closure: () -> ()) { // function body goes here } // here's how you call this function without using a trailing closure: someFunctionThatTakesAClosure({ // closure's body goes here }) // here's how you call this function with a

Swift 3 : URL Image makes UITableView scroll slow issue

我怕爱的太早我们不能终老 提交于 2019-12-01 12:03:29
I have an extension to print image URL on UIImageView . But I think the problem is my tableView is so slow because of this extension. I think I need to open thread for it. How can I create a thread in this extension or do you know another solution to solve this problem? My code : extension UIImageView{ func setImageFromURl(stringImageUrl url: String){ if let url = NSURL(string: url) { if let data = NSData(contentsOf: url as URL) { self.image = UIImage(data: data as Data) } } } } I think, that problem here, that you need to cache your images in table view to have smooth scrolling. Every time

Objective C - Unit testing dispatch_async block?

眉间皱痕 提交于 2019-11-30 06:44:42
I read other posts that come up with solutions to this question. However, their solutions require hacky code to be added to my application in order to be able to test it. To me clean code is more important than unit test. I use dispatch_async in my app regularly, and I'm having trouble unit testing it. The problem is the block executes after my test is already done, since it's running asynchronously on the main queue. Is there a way to somehow wait until the block is executed and then continue with the test. I DON'T want to pass a completion to the block only because of unit-testing - (viod

dispatch_async and block in iOS

∥☆過路亽.° 提交于 2019-11-29 23:46:47
What this piece of code mean? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ TMBaseParser *parser=[[TMBaseParser alloc] init]; parser.delegate=self; NSString *post =nil; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; [parser parseForServiceType:TMServiceCategories postdata:postData]; }); please explain it briefly. Marcin Kuptel The piece of code in dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ }); is run asynchronously on a background thread. This is done because parsing data may be a time consuming task

Swift 2 to 3 Migration dispatch_get_global_queue [duplicate]

南笙酒味 提交于 2019-11-29 12:26:50
This question already has an answer here: How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond? 6 answers I have the following code that I've been trying to translate into swift 3 from swift 2. Here is what I have so far. DispatchQueue.async(group: DispatchQueue.global(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),execute: { self.controllerDelegate?.codeToRun(progressWindowViewController: self) }) I am getting an error that says Cannot invoke 'global' with an argument list of type (int,int). I know that the global queue needs this though unless they changed it in

Objective C - Unit testing dispatch_async block?

梦想与她 提交于 2019-11-29 06:27:30
问题 I read other posts that come up with solutions to this question. However, their solutions require hacky code to be added to my application in order to be able to test it. To me clean code is more important than unit test. I use dispatch_async in my app regularly, and I'm having trouble unit testing it. The problem is the block executes after my test is already done, since it's running asynchronously on the main queue. Is there a way to somehow wait until the block is executed and then

dispatch_async and block in iOS

心不动则不痛 提交于 2019-11-28 20:55:21
问题 What this piece of code mean? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ TMBaseParser *parser=[[TMBaseParser alloc] init]; parser.delegate=self; NSString *post =nil; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; [parser parseForServiceType:TMServiceCategories postdata:postData]; }); please explain it briefly. 回答1: The piece of code in dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ }); is run

Multiple async requests at once in objective c

荒凉一梦 提交于 2019-11-28 12:58:47
I have am downloading multiple images from a web server at the same time, and I the problem is they are getting mixed up with each other. dispatch_async(dispatch_get_global_queue(0,0), ^{ NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"website.com"]]; if (data == nil) return; dispatch_async(dispatch_get_main_queue(), ^{ cell.imageView.image = [UIImage imageWithData:data]; }); }); I got this code from: Getting Image from URL Objective C . How do i fix this?? also is there any way to speed up download speeds? You can use "AsyncImageView" class files it will load

Why does it take such a long time for UI to be updated from background thread?

笑着哭i 提交于 2019-11-28 11:38:11
I understand that all UI updates must be done from Main thread. But purely for the sake of deeper understanding how GCD and dispatch main work: I have a button that runs a network call and in its completionHandler I eventually do: self.layer.borderColor = UIColor(red: 255/255.0, green: 59/255.0, blue: 48/255.0, alpha: 1.0).cgColor self.layer.borderWidth = 3.0 For the color change to happen it takes 6-7 seconds. Obviously if run the above code from main thread it would change the border color immediately. Question1 even though I don't have ANY other code to run, why doesn't the UI changes