grand-central-dispatch

Swift: Simple DispatchQueue does not run & notify correctly

老子叫甜甜 提交于 2020-05-29 16:58:05
问题 What i am doing wrong? At playground it runs as it should. But as soon as i deploy it on iOS simulator it returns the wrong sequence. @objc func buttonTapped(){ let group = DispatchGroup() let dispatchQueue = DispatchQueue.global(qos: .default) for i in 1...4 { group.enter() dispatchQueue.async { print("🔹 \(i)") } group.leave() } for i in 1...4 { group.enter() dispatchQueue.async { print("❌ \(i)") } group.leave() } group.notify(queue: DispatchQueue.main) { print("jobs done by group") } }

How many is too many for create dispatch_queues in GCD (grand central dispatch)?

邮差的信 提交于 2020-05-25 16:20:50
问题 There is a wonderful article about a lightweight notification system built in Swift, by Mike Ash: (https://www.mikeash.com/pyblog/friday-qa-2015-01-23-lets-build-swift-notifications.html). The basic idea is you create objects that you can "listen" to i.e. invoke a callback on when there is some state change. To make it thread-safe, each object created holds its own dispatch_queue. The dispatch_queue is simply used to gate critical sections: dispatch_sync(self.myQueue) { // modify critical

How many is too many for create dispatch_queues in GCD (grand central dispatch)?

。_饼干妹妹 提交于 2020-05-25 16:19:27
问题 There is a wonderful article about a lightweight notification system built in Swift, by Mike Ash: (https://www.mikeash.com/pyblog/friday-qa-2015-01-23-lets-build-swift-notifications.html). The basic idea is you create objects that you can "listen" to i.e. invoke a callback on when there is some state change. To make it thread-safe, each object created holds its own dispatch_queue. The dispatch_queue is simply used to gate critical sections: dispatch_sync(self.myQueue) { // modify critical

How to Move Timer to Background Thread

北慕城南 提交于 2020-05-10 04:26:04
问题 I currently have two NSTimer timers in my app that are responsible for taking data and updating the UI. I have noticed recently that while the timers are running I have poor UI performance, for example the user can't scroll through my UITableview well or at all. I've read elsewhere that it is possible to push the timers into a different runloop and that this could help. This is what my timer looks like now: let aSelector : Selector = "updateLocation" timer = NSTimer

SwiftUI async Data fetch

南楼画角 提交于 2020-03-25 17:53:00
问题 I am trying to learn SwiftUI and creating a movie search app with The movie Database API I would like to fetch new data once the scroll goes at the end of the List. I found a possible solution on SO using ForEach inside the List and checking when the list reaches the last item and then performing the onAppear() call. In SwiftUI, where are the control events, i.e. scrollViewDidScroll to detect the bottom of list data How can I load new pages from the search when the first page has loaded? You

SwiftUI async Data fetch

被刻印的时光 ゝ 提交于 2020-03-25 17:52:45
问题 I am trying to learn SwiftUI and creating a movie search app with The movie Database API I would like to fetch new data once the scroll goes at the end of the List. I found a possible solution on SO using ForEach inside the List and checking when the list reaches the last item and then performing the onAppear() call. In SwiftUI, where are the control events, i.e. scrollViewDidScroll to detect the bottom of list data How can I load new pages from the search when the first page has loaded? You

How can you use Dispatch Groups to wait to call multiple functions that depend on different data?

☆樱花仙子☆ 提交于 2020-02-21 04:54:15
问题 I have three variables, a , b and c . I have three asynchronous functions with completion blocks to update these variables and three more functions that do some work with only some of the data. I'm making sure that the working functions wait until all the data is updated with a DispatchGroup . // The Data var a: String? var b: String? var c: String? // The Update let group = DispatchGroup() group.enter() updateA() { group.leave() } group.enter() updateB() { group.leave() } group.enter()

Ocassional crash on the app start

天涯浪子 提交于 2020-02-05 03:26:46
问题 I'm getting very strange crash at the start of the app. My app spawns a lot of threads (over 500, most of them are related to PLPreheatItem, I can not paste full crash log here, cause stackoverflow has limit to 30K symbols). The problem is that I can not reproduce it on my device. Also it happens on clean install for my customer almost every run. I am using NSURLSession with background session and NSOperationQueue with 4 concurrent operations. These are the most weak parts as I think. Could

How to use dispatch_async_f?

大城市里の小女人 提交于 2020-02-03 05:02:28
问题 The function that I want queued takes no parameters. What do I pass in as paramContext ? Passing in NULL generates the compile error "Invalid use of void expression". I do not want to add a parameter to my function just to make it compile - how do I make this work? Mac OS X Snowleopard, Xcode 3.2.6 with Objective-C 回答1: You need to wrap the function somehow. The easiest way is actually to use dispatch_async() instead, as in dispatch_async(queue, ^{ myFunc() }); 回答2: While you can just pass 0

wait an async callback to sync

血红的双手。 提交于 2020-01-25 06:31:04
问题 I want to wrap a SDK async api to sync, code looks like this: dispatch_semaphore_t sema = dispatch_semaphore_create(0); __block BOOL _isLogined; __block BOOL _isCallback = NO; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^ { //Put your heavy code here it will not block the user interface [[SDKPlatform defaultPlatform] SDKIsLogined:^(BOOL isLogined){ _isLogined = isLogined; _isCallback = YES; dispatch_semaphore_signal(sema); }];