grand-central-dispatch

Waiting for multiple blocks to finish

 ̄綄美尐妖づ 提交于 2019-12-02 19:29:12
I have those methods to retrieve some object information from the internet: - (void)downloadAppInfo:(void(^)())success failure:(void(^)(NSError *error))failure; - (void)getAvailableHosts:(void(^)())success failure:(void(^)(NSError *error))failure; - (void)getAvailableServices:(void(^)())success failure:(void(^)(NSError *error))failure; - (void)getAvailableActions:(void(^)())success failure:(void(^)(NSError *error))failure; The downloaded stuff gets stored in object properties, so that is why the success functions return nothing. Now, I want to have one method like this: - (void)syncEverything:

In Swift, how to stop all the process until datas retrieved from parse.com in UICOLLECTIONVIEW

天大地大妈咪最大 提交于 2019-12-02 18:28:31
问题 In CollectionView I am displaying datas from parse.com. Successfully retrieved. But unable to display in the cell. I am receiving error as Array outbound. I found out the mistake, parse is running as asynchronous. But, before parse end, collection view gets loaded. So I unable to display values in the cell. It is throwing an error. How to stop all the process until parse get loaded completely? Kindly guide me. MY CODING IS BELOW: //VIEWCONTROLLER having collectionView var myList : NSArray =

What will happen if I have nested dispatch_async calls?

淺唱寂寞╮ 提交于 2019-12-02 18:06:41
It may be a dumb question but I need to ask and clear this up for myself. To submit a block onto a queue for execution, use the functions dispatch_sync and dispatch_async . They both take a queue and a block as parameters. dispatch_async returns immediately, running the block asynchronously, while dispatch_sync blocks execution until the provided block returns. Here are some situations: Situation 1 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); dispatch_async(queue, ^{ [self goDoSomethingLongAndInvolved]; dispatch_async(queue, ^{ NSLog(@"this is

iOS GCD: Difference between any global queue and the one with background priority (DISPATCH_QUEUE_PRIORITY_BACKGROUND)?

痞子三分冷 提交于 2019-12-02 17:55:01
I am reading Concurrency Programming Guide and things confuse me. I see a lot of code invoking the following for any background task: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); Now what I mean by 'background' is the popular meaning: Something that gets executed anywhere other than the main (UI) thread So following the docs, the above statement returns any non-main-thread queue with differing priorities. My question is - why does then DISPATCH_QUEUE_PRIORITY_BACKGROUND exist? Lately I also see many async tasks using DISPATCH_QUEUE_PRIORITY_BACKGROUND specifically to perform

When to use Semaphore instead of Dispatch Group?

帅比萌擦擦* 提交于 2019-12-02 17:45:09
I would assume that I am aware of how to work with DispatchGroup , for understanding the issue, I've tried: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() performUsingGroup() } func performUsingGroup() { let dq1 = DispatchQueue.global(qos: .userInitiated) let dq2 = DispatchQueue.global(qos: .userInitiated) let group = DispatchGroup() group.enter() dq1.async { for i in 1...3 { print("\(#function) DispatchQueue 1: \(i)") } group.leave() } group.wait() dq2.async { for i in 1...3 { print("\(#function) DispatchQueue 2: \(i)") } } group.notify(queue:

Core Data saving objects in background issue

。_饼干妹妹 提交于 2019-12-02 17:21:49
What I'm trying todo in a nutshell is I am using a background queue to save JSON objects pulled from a web service to the Core Data Sqlite3 database. The saving takes place on a serialized background queue I've created via GCD, and saved to a secondary instance of NSManagedObjectContext that is created for that background queue. Once the save is complete I need to update the instance of NSManagedObjectContext that is on the main thread with the newly created/updated objects. The problem I am having though is the instance of NSManagedObjectContext on the main thread is not able to find the

Which is the best of GCD, NSThread or NSOperationQueue? [closed]

纵饮孤独 提交于 2019-12-02 16:19:46
What's the best way of multithreading in iOS as we have three options GCD, NSThread , and NSOperationQueue ? I am confused in which one is the best? If none, then which should be used in what scenario and how they differ and also, if someone has some good example of using NSOperationQueue , please share so that I can learn. Simple answer: Use NSThread (or even the pthreads API) when you want or need to have direct control over the threads you create, e.g. you need fine-grained control over thread priorities or are interfacing with some other subsystem that vends/consumes thread objects

need some clarifications about dispatch queue, thread and NSRunLoop

天大地大妈咪最大 提交于 2019-12-02 15:54:53
The following things are what I know & understand: Global queue is a concurrent queue which can dispatch tasks to multiple threads. The order of executing task is not guaranteed. e.g.: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { for (int i; i<10; i++) { doTask() } }) If I want to dispatch to serial queue, I can use dispatch_async(dispatch_queue_create("my.serial.queue", nil) { ... } each time only one task is dispatched to a thread & get executed. The order is FIFO. ===== What I am confused & not fully understand ======= The main thread has a NSRunLoop,

GCD Poor Performance

99封情书 提交于 2019-12-02 14:58:49
As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because all of my GCD code is significantly slower and less consistent than my serial code (between 1.4x and 10x slower). Allow me to give you an example: I am iterating over an array in a bubble-sort fashion to determine all possible collisions among objects in that array: - (double) detectCollisionsInArray:(NSArray*)objects { int count = [objects count]; if (count > 0) { double time = CFAbsoluteTimeGetCurrent(); for (int i =

How do I use DispatchGroup / GCD to execute functions sequentially in swift?

强颜欢笑 提交于 2019-12-02 14:17:41
问题 I need to run two functions sequentially before I refresh my collection view. The first function pulls autoids from firebase and writes them into an array. The second function (getLocation) then uses that array to make another call to firebase to retrieve a value (location) beneath each autoid. I'm using DispatchGroup() to ensure the first function finishes before the second one begins. But I also need the second function to complete before the notify refreshes the collection view. I've