dispatch-async

Max values of semaphore?

只愿长相守 提交于 2021-01-01 06:49:08
问题 For example, there is a 1000 times loop. What's the max value to make it fast, effective, and not lead to a deadlock? let group = DispatchGroup() let queue = DispatchQueue(label: "com.num.loop", attributes: .concurrent) let semaphore = DispatchSemaphore(value: 4) for i in 1...1000 { semaphore.wait() group.enter() queue.async(group: group, execute: { doWork(i) group.leave() semaphore.signal() }) } group.notify(queue: DispatchQueue.main) { // go on... } 回答1: A couple of observations: You never

Max values of semaphore?

こ雲淡風輕ζ 提交于 2021-01-01 06:48:27
问题 For example, there is a 1000 times loop. What's the max value to make it fast, effective, and not lead to a deadlock? let group = DispatchGroup() let queue = DispatchQueue(label: "com.num.loop", attributes: .concurrent) let semaphore = DispatchSemaphore(value: 4) for i in 1...1000 { semaphore.wait() group.enter() queue.async(group: group, execute: { doWork(i) group.leave() semaphore.signal() }) } group.notify(queue: DispatchQueue.main) { // go on... } 回答1: A couple of observations: You never

DispatchQueue threads don't always set the correct results

别等时光非礼了梦想. 提交于 2020-12-13 03:09:07
问题 Am trying on coding game MineSweeper, the following code is to set the numbers around landmines. For a test, I choose the minimum level 9 x 9 with 10 landmines. For a faster performance, I tried to use more threads when setting numbers, but one day I found that it doesn't always give the correct number arrangements, I made a loop to create it 1000 times and found that 20 ~ 40 out of 1000 are wrong. Here are several wrong results, "*" represents landmine, "0" means no landmine around wrong

React + Redux - dispatching an action in dumb component?

家住魔仙堡 提交于 2020-02-24 10:53:28
问题 I started learning Redux and the whole idea looks neat, but after rebuilding my React app from "normal" to "redux-way" this problem came up. I have a list of if items that I build based on JSON from async call. Then every item on that list sends an async call on click and returns something. Before my app was pretty straightforward: components/list/List.jsx components/list/ListItem.jsx Right now it looks like this: footer/list/ListContainer.jsx // here I run an async call and generate list

Swift 3 : URL Image makes UITableView scroll slow issue

五迷三道 提交于 2020-01-21 05:25:12
问题 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) } } } } 回答1: I think,

Need clarification on dispatch_group_wait() behavior when dispatch_group_create() and dispatch_group_enter() are called from different queues

风格不统一 提交于 2020-01-17 01:31:06
问题 I am looking at the Ray Wenderlich tutorial on using dispatch queues to get notified when a group of tasks complete. http://www.raywenderlich.com/63338/grand-central-dispatch-in-depth-part-2 The first code shown under "Code that works" is straight from the tutorial. The Alert view(final completion block) get executed after all 3 downloads complete. I tried to play around with it and moved the dispatch async down in the "Code that does not work" to see what will happen if dispatch_group_create

Asynchronous swift 3

梦想与她 提交于 2020-01-15 09:20:06
问题 I need to make an asynchronous call so that the second method is only called after the first one is completed.Both methods are network calls. Something like this: signIn() getContacts() I want to make sure that getContacts only gets called after the signIn is completed. FWIW, I can't edit the methods signatures because they are from a Google SDK. This is what I tried: let queue = DispatchQueue(label: "com.app.queue") queue.async { signIn() getContacts() } 回答1: Async calls, by their nature, do

DispatchQueue crashing with main.sync in Swift

陌路散爱 提交于 2020-01-12 05:43:30
问题 Please explain to me why I am getting this crash? Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) in this DispatchQueue.main.sync { print("sync") } This is my code. override func viewDidLoad() { super.viewDidLoad() print("Start") DispatchQueue.main.async { print("async") } DispatchQueue.main.sync { print("sync") } print("Finish") } 回答1: @sankalap, Dispatch.main is a serial queue which has single thread to execute all the operations. If we call "sync" on this queue it will

TableView Cell show image with dispatch_async shows “unexpected non-void return value in void function” issue

谁都会走 提交于 2019-12-25 20:57:20
问题 I want to show the image in the TableViewCell. There are the codes: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "myCell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DIYSquareALLCell cell.titles!.text = titles[indexPath.row] cell.leftImages!.image = getPic(leftImages[indexPath.row]) return cell } func getPic(PicURL: String) -> UIImage! { let image = self

NSUrlSession Running Slow in Swift

五迷三道 提交于 2019-12-25 03:39:14
问题 In swift I am downloading data in swift to my iOS app. It works just fine although what ends up happening is it can take up to 20 seconds for it to load even though I am on a fast connection. I don't understand why this happens. I was almost thinking about downloading all the data before the app opens although I don't want to do that because I know it is possible to speed it up which I know is possible because apps like YouTube and Facebook can load and refresh in less than 20 seconds. Heck,