grand-central-dispatch

Does dispatch_async(dispatch_get_main_queue(), ^{…}); wait until done?

淺唱寂寞╮ 提交于 2019-11-26 08:48:31
问题 I have a scenario in my app, where I want to do some time consuming task which consists of some data processing as well as UI update, in a method. My method looks like this, - (void)doCalculationsAndUpdateUIs { // DATA PROCESSING 1 // UI UPDATE 1 // DATA PROCESSING 2 // UI UPDATE 2 // DATA PROCESSING 3 // UI UPDATE 3 } As it is time consuming I wanted to do the data processing on the background thread, using, dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

Why can't we use a dispatch_sync on the current queue?

ぐ巨炮叔叔 提交于 2019-11-26 08:48:27
问题 I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn\'t know which until runtime (using StoreKit.framework ). I also had UI code that I needed to update in that callback which needed to happen before the function executed, so my initial thought was to have a function like this: -(void) someDelegateCallback:(id) sender { dispatch_sync(dispatch_get_main_queue(), ^{ // ui update code here }); // code here that depends

Adding items to Swift array across multiple threads causing issues (because arrays aren't thread safe) - how do I get around that?

天大地大妈咪最大 提交于 2019-11-26 08:26:06
问题 I want to add given blocks to an array, and then run all the blocks contained in the array, when requested. I have code similar to this: class MyArrayBlockClass { private var blocksArray: Array<() -> Void> = Array() private let blocksQueue: NSOperationQueue() func addBlockToArray(block: () -> Void) { self.blocksArray.append(block) } func runBlocksInArray() { for block in self.blocksArray { let operation = NSBlockOperation(block: block) self.blocksQueue.addOperation(operation) } self

How can I retrieve a return value from a completion block?

不羁的心 提交于 2019-11-26 08:13:33
问题 Is it possible to run a completion block on the main thread? For example, I have one method which returns a value: - (int)test { /* here one method is called with completion block with return type void */ [obj somemethodwithcompeltionblock: { /* here I am getting my Int which I want to return */ } ]; } but I can\'t see how to return the integer value from within the completion block as the result of this method, because the completion block runs on a background thread. How can I do this? 回答1:

Workaround on the threads limit in Grand Central Dispatch?

淺唱寂寞╮ 提交于 2019-11-26 08:08:18
问题 With Grand Central Dispatch, one can easily perform time consuming task on non-main thread, avoid blocking the main thead and keep the UI responsive. Simply by using dispatch_async and perform the task on a global concurrent queue. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // code }); However, something sounds too good to be true like this one usually have their downside. After we use this a lot in our iOS app project, recently we found that there\'s a

Suspending GCD query problem

筅森魡賤 提交于 2019-11-26 08:06:17
问题 i have trouble suspending a gcd query. Here is some code that demonstrates the problem: static dispatch_queue_t q=nil; static void test(int a){ if(q){ dispatch_suspend(q); dispatch_release(q); q=nil; } q=dispatch_get_global_queue(0,0); dispatch_async(q,^ { while(1){NSLog(@\"query %d\",a);sleep(2);} }); } int main(int argc, const char* argv[]){ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; test(1); //blah blah blah test(2); while(1){} [pool release]; return 0; } What I\'m trying

Difference between dispatch_async and dispatch_sync on serial queue?

馋奶兔 提交于 2019-11-26 07:52:28
问题 I\'ve created a serial queue like this: dispatch_queue_t _serialQueue = dispatch_queue_create(\"com.example.name\", DISPATCH_QUEUE_SERIAL); What\'s the difference between dispatch_async called like this dispatch_async(_serialQueue, ^{ /* TASK 1 */ }); dispatch_async(_serialQueue, ^{ /* TASK 2 */ }); And dispatch_sync called like this on this serial queue? dispatch_sync(_serialQueue, ^{ /* TASK 1 */ }); dispatch_sync(_serialQueue, ^{ /* TASK 2 */ }); My understanding is that, regardless of

What&#39;s the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

六月ゝ 毕业季﹏ 提交于 2019-11-26 06:14:54
问题 For the longest time I thought asynchronous was synonymous to running something on a background thread, while synchronous meant on the main thread (blocking UI updates and interactions). I understand that not running on the main thread for expensive actions is because it doesn\'t allow UI actions to occur as the main thread is occupied, but why is synchronous troublesome? However, it\'s since came to my attention that you can make asynchronous calls on the main thread, and synchronous calls

Can I declare dispatch_once_t predicate as a member variable instead of static?

我们两清 提交于 2019-11-26 06:07:19
问题 I want to run a block of code only once per instance. Can I declare dispatch_once_t predicate as a member variable instead of static variable? From GCD Reference, it is not clear to me. The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined. I know I can use dispatch_semaphore_t and a boolean flag to do the same thing. I\'m just curious. 回答1: dispatch_once_t must not be an instance variable. The

Wait until swift for loop with asynchronous network requests finishes executing

眉间皱痕 提交于 2019-11-26 05:48:04
I would like a for in loop to send off a bunch of network requests to firebase, then pass the data to a new view controller once the the method finishes executing. Here is my code: var datesArray = [String: AnyObject]() for key in locationsArray { let ref = Firebase(url: "http://myfirebase.com/" + "\(key.0)") ref.observeSingleEventOfType(.Value, withBlock: { snapshot in datesArray["\(key.0)"] = snapshot.value }) } // Segue to new view controller here and pass datesArray once it is complete I have a couple concerns. First, how do I wait until the for loop is finished and all the network