grand-central-dispatch

Pattern for unit testing async queue that calls main queue on completion

心不动则不痛 提交于 2019-11-26 19:09:10
问题 This is related to my previous question, but different enough that I figured I'd throw it into a new one. I have some code that runs async on a custom queue, then executes a completion block on the main thread when complete. I'd like to write unit test around this method. My method on MyObject looks like this. + (void)doSomethingAsyncThenRunCompletionBlockOnMainQueue:(void (^)())completionBlock { dispatch_queue_t customQueue = dispatch_queue_create("com.myObject.myCustomQueue", 0); dispatch

Grand Central Dispatch vs NSThreads?

。_饼干妹妹 提交于 2019-11-26 18:57:04
问题 I searched a variety of sources but don't really understand the difference between using NSThreads and GCD. I'm completely new to the OS X platform so I might be completely misinterpreting this. From what I read online, GCD seems to do the exact same thing as basic threads (POSIX, NSThreads etc.) while adding much more technical jargon ("blocks"). It seems to just overcomplicate the basic thread creation system (create thread, run function). What exactly is GCD and why would it ever be

What's the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

早过忘川 提交于 2019-11-26 18:13:44
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 on background threads. I always hear people saying not to use expensive calls synchronously or on the main

How to asynchronously load an image in an UIImageView?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 18:03:54
问题 I have an UIView with an UIImageView subview. I need to load an image in the UIImageView without blocking the UI. The blocking call seems to be: UIImage imageNamed: . Here is what I thought solved this problem: -(void)updateImageViewContent { dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ UIImage * img = [UIImage imageNamed:@"background.jpg"]; dispatch_sync(dispatch_get_main_queue(), ^{ [[self imageView] setImage:img]; }); }); } The image is small

How do you schedule a block to run on the next run loop iteration?

风流意气都作罢 提交于 2019-11-26 17:55:19
问题 I want to be able to execute a block on the next run loop iteration. It's not so important whether it gets executed at the beginning or the end of the next run loop, just that execution is deferred until all code in the current run loop has finished executing. I know the following doesn't work because it gets interleaved with the main run loop so my code might execute on the next run loop but it might not. dispatch_async(dispatch_get_main_queue(),^{ //my code }); The following I believe

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

心已入冬 提交于 2019-11-26 17:49:34
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. dispatch_once_t must not be an instance variable. The implementation of dispatch_once() requires that the dispatch_once_t is zero, and has never been non-zero .

dispatch_once after the Swift 3 GCD API changes

北城余情 提交于 2019-11-26 17:30:12
问题 What is the new syntax for dispatch_once in Swift after the changes made in language version 3? The old version was as follows. var token: dispatch_once_t = 0 func test() { dispatch_once(&token) { } } These are the changes to libdispatch that were made. 回答1: From the doc: Dispatch The free function dispatch_once is no longer available in Swift. In Swift, you can use lazily initialized globals or static properties and get the same thread-safety and called-once guarantees as dispatch_once

Grand Central Dispatch (GCD) vs. performSelector - need a better explanation

我与影子孤独终老i 提交于 2019-11-26 17:12:25
I've used both GCD and performSelectorOnMainThread:waitUntilDone in my apps, and tend to think of them as interchangeable--that is, performSelectorOnMainThread:waitUntilDone is an Obj-C wrapper to the GCD C syntax. I've been thinking of these two commands as equivalent: dispatch_sync(dispatch_get_main_queue(), ^{ [self doit:YES]; }); [self performSelectorOnMainThread:@selector(doit:) withObject:YES waitUntilDone:YES]; Am I incorrect? That is, is there a difference of the performSelector* commands versus the GCD ones? I've read a lot of documentation on them, but have yet to see a definitive

Block_release deallocating UI objects on a background thread

孤街浪徒 提交于 2019-11-26 16:59:22
问题 One of the patterns presented at the WWDC 2010 "Blocks and Grand Central Dispatch" talk was to use nested dispatch_async calls to perform time consuming tasks on a background thread and then update the UI on the main thread once the task is complete dispatch_async(backgroundQueue, ^{ // do something time consuming in background NSArray *results = ComputeBigKnarlyThingThatWouldBlockForAWhile(); // use results on the main thread dispatch_async(dispatch_get_main_queue(), ^{ [myViewController

Prevent dispatch_after() background task from being executed

半腔热情 提交于 2019-11-26 16:44:22
问题 This is my issue. When my application enters background I want it to perform a function after certain period of time. This is what I do: - (void)applicationDidEnterBackground:(UIApplication *)application { isRunningInBackground = YES; taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; int64_t delayInSeconds = 30; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_global