grand-central-dispatch

How can I be notified when a dispatch_async task is complete?

别等时光非礼了梦想. 提交于 2019-11-30 12:29:39
I have a asynchronous task like so: dispatch_async(dispatch_get_main_queue(), ^{ myAsyncMethodsHere; }); Is there a way to be notified when the background task is complete? Or to call a method upon completion? I've read through the documentation and have looked into dispatch_after, but it seems to be more designed to dispatch the method after a certain length of time. Thanks for the help. From the docs: COMPLETION CALLBACKS Completion callbacks can be accomplished via nested calls to the dispatch_async() function. It is important to remember to retain the destination queue before the first

Very slow HTML rendering in NSAttributedString

ε祈祈猫儿з 提交于 2019-11-30 09:11:22
I have UITableView with dynamic sizing cells that displays list of comments in HTML format and I faced with the problem that NSAttributedString renders HTML content extremely slow! Here is snapshot from profiler. I tried to put the NSAttributedString initialization to separate thread, but still slow and user sees empty cells while HTML is being rendered and finally when it finished rendering cell is not layout properly. dispatch_async(GlobalQueue, { let html = NSAttributedString( data: self.comment.htmlBody.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false)!, options:

Suggested resources for learning about blocks

人盡茶涼 提交于 2019-11-30 09:09:44
What are some good suggested resources for learning about blocks and GCD in Mac OS X and iOS I would start with Apple's Blocks Programming Topics document or with Programming with Blocks on Apple Devices . As gs mentioned, also check out Mike Ash's articles (and also subscribe to his RSS feed, as I'm sure more blocks-related posts will come up): Friday Q&A 2008-12-26 Friday Q&A 2009-08-14: Practical Blocks Friday Q&A 2009-08-28: Intro to Grand Central Dispatch Part 1 There is a great introduction at bbum's weblog: Basic Blocks Mike Ash has written two articles about blocks in his blog: Friday

What GCD queue, main or not, am I running on?

十年热恋 提交于 2019-11-30 09:09:33
I am trying to write some thread safe methods so I am using: ... dispatch_queue_t main = dispatch_get_main_queue(); dispatch_sync(main,^{ [self doSomethingInTheForeground]; }); ... But If I am on the main thread that is not necessary, and I can skip all those dispatch calls, so I would like to know what thread I currently am on. How can I know this? Or, perhaps it does not make difference (in performance) doing it? Is it ok to do this comparison? if (dispatch_get_main_queue() == dispatch_get_current_queue()){...} Updated answer : The Apple docs have changed and now say "When called from

Read-Write lock with GCD

社会主义新天地 提交于 2019-11-30 09:03:42
My application makes heavy use of GCD, and almost everything is split up in small tasks handled by dispatches. However, the underlying data model is mostly read and only occasionally written. I currently use locks to prevent changes to the critical data structures while reading. But after looking into locks some more today, I found NSConditionLock and some page about read-write locks. The latter is exactly what I need. I found this implementation: http://cocoaheads.byu.edu/wiki/locks . My question is, will this implementation work with GCD, seeing that it uses PThreads? It will still work.

Is there any way to implement dispatch_cancel()?

*爱你&永不变心* 提交于 2019-11-30 08:45:51
问题 So far I have gone through the doc of GCD, however it seems there misses dispatch_cancel() which I want to use it to cancel all dispatch's blocks invocation. Is there any way to implement dispatch_cancel()? 回答1: As @HampusNilsson mentions, you can't reasonably cancel any in-flight operation in a non-garbage collected environment (such as this) because it would inherently leak resources and leave the process in an indeterminate state. NSOperationQueue has a cancellation API, and that API can

Grand Central Strategy for Opening Multiple Files

风格不统一 提交于 2019-11-30 07:38:26
I have a working implementation using Grand Central dispatch queues that (1) opens a file and computes an OpenSSL DSA hash on "queue1", (2) writing out the hash to a new "side car" file for later verification on "queue2". I would like to open multiple files at the same time, but based on some logic that doesn't "choke" the OS by having 100s of files open and exceeding the hard drive's sustainable output. Photo browsing applications such as iPhoto or Aperture seem to open multiple files and display them, so I'm assuming this can be done. I'm assuming the biggest limitation will be disk I/O, as

Why am I getting deadlock with dispatch_once?

ε祈祈猫儿з 提交于 2019-11-30 07:25:06
Why am I deadlocking? - (void)foo { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self foo]; }); // whatever... } I expect foo to be executed twice on first call. Neither of the existing answers are quite accurate (one is dead wrong, the other is a bit misleading and misses some critical details). First, let's go right to the source : void dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func) { struct _dispatch_once_waiter_s * volatile *vval = (struct _dispatch_once_waiter_s**)val; struct _dispatch_once_waiter_s dow = { NULL, 0 }; struct _dispatch_once

Updating cell height after image downloads

感情迁移 提交于 2019-11-30 06:55:55
问题 I am displaying some text and images in a UITableView . The image first gets downloaded. Since before the image gets downloaded, I don't know the size of image, so I initially put a UIImageView of some fixed size. And when the image is downloaded, I resize the UIImageView . dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Download image dispatch_async(dispatch_get_main_queue(), ^{ // UIImageView resizing }); }); All this happens in cellForRowAtIndexPath .

Performance test: sem_t v.s. dispatch_semaphore_t and pthread_once_t v.s. dispatch_once_t

白昼怎懂夜的黑 提交于 2019-11-30 06:45:57
I wanted to know what would be better/faster to use POSIX calls like pthread_once() and sem_wait() or the dispatch_* functions, so I created a little test and am surprised at the results (questions and results are at the end). In the test code I am using mach_absolute_time() to time the calls. I really don’t care that this is not exactly matching up with nano-seconds; I am comparing the values with each other so the exact time units don't matter, only the differences between the interval do. The numbers in the results section are repeatable and not averaged; I could have averaged the times but