grand-central-dispatch

Last In-First Out Stack with GCD?

邮差的信 提交于 2019-11-27 00:00:21
I have a UITableView that displays images associated with contacts in each row. In some cases these images are read on first display from the address book contact image, and where there isn't one they are an avatar rendered based on stored data. I presently have these images being updated on a background thread using GCD. However, this loads the images in the order they were requested, which means during rapid scrolling the queue becomes lengthy and when the user stops scrolling the current cells are the last to get updated. On the iPhone 4, the problem isn't really noticeable, but I am keen

Why should I choose GCD over NSOperation and blocks for high-level applications?

回眸只為那壹抹淺笑 提交于 2019-11-26 23:58:22
问题 Apple's Grand Central Dispatch reference says: "...if your application needs to operate at the Unix level of the system—for example, if it needs to manipulate file descriptors, Mach ports, signals, or timers. GCD is not restricted to system-level applications, but before you use it for higher-level applications, you should consider whether similar functionality provided in Cocoa (via NSOperation and block objects) would be easier to use or more appropriate for your needs.". http://developer

Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?

谁说胖子不能爱 提交于 2019-11-26 23:54:24
I have a method that accepts a block and a completion block. The first block should run in the background, while the completion block should run in whatever queue the method was called. For the latter I always used dispatch_get_current_queue() , but it seems like it's deprecated in iOS 6 or higher. What should I use instead? Catfish_Man The pattern of "run on whatever queue the caller was on" is appealing, but ultimately not a great idea. That queue could be a low priority queue, the main queue, or some other queue with odd properties. My favorite approach to this is to say "the completion

Grand Central Dispatch (GCD) with CoreData

喜夏-厌秋 提交于 2019-11-26 23:52:54
问题 I'm using Grand Central Dispatch (GCD) in my application to do some heavy lifting. The application is using Core-Data for data storage purposes. Here's my scenario (along with relevant question): dispatch_queue_t main_queue = dispatch_get_main_queue(); dispatch_queue_t request_queue = dispatch_queue_create("com.app.request", NULL); dispatch_async(request_queue, ^{ MyNSManagedObject *mObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; // … /

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

孤街醉人 提交于 2019-11-26 23:50:54
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), ^{ But as both data processing and UI updates are in the same method, I wanted to move only the UI updates

In Swift how to call method with parameters on GCD main thread?

大城市里の小女人 提交于 2019-11-26 23:47:55
In my app I have a function that makes an NSRURLSession and sends out an NSURLRequest using sesh.dataTaskWithRequest(req, completionHandler: {(data, response, error) In the completion block for this task, I need to do some computation that adds a UIImage to the calling viewcontroller. I have a func called func displayQRCode(receiveAddr, withAmountInBTC:amountBTC) that does the UIImage-adding computation. If I try to run the view-adding code inside of the completion block, Xcode throws an error saying that I can't use the layout engine while in a background process. So I found some code on SO

Dispatch queues: How to tell if they're running and how to stop them

隐身守侯 提交于 2019-11-26 23:43:33
I'm just playing around with GCD and I've written a toy CoinFlipper app. Here's the method that flips the coins: - (void)flipCoins:(NSUInteger)nFlips{ // Create the queues for work dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL); // Split the number of flips into whole chunks of kChunkSize and the remainder. NSUInteger numberOfWholeChunks = nFlips / kChunkSize; NSUInteger numberOfRemainingFlips = nFlips - numberOfWholeChunks * kChunkSize; if (numberOfWholeChunks > 0) { for (NSUInteger index = 0;

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

安稳与你 提交于 2019-11-26 23:40:25
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 upon the UI getting updated } That works great, when it is executed on the background thread. However,

NSThread vs. NSOperationQueue vs. ??? on the iPhone

半腔热情 提交于 2019-11-26 22:33:32
问题 Currently I'm using NSThread to cache images in another thread. [NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:self withObject:image]; Alternately: [self performSelectorInBackground:@selector(cacheImage:) withObject:image]; Alternately, I can use an NSOperationQueue NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(cacheImage:) object:image]; NSOperationQueue *opQueue = [[NSOperationQueue alloc] init]; [opQueue

dispatch_sync() always execute block in main thread

五迷三道 提交于 2019-11-26 22:17:52
问题 Is there any difference between if dispatch_sync is called in 3 different queue like 1. dispatch_sync(dispatch_get_main_queue(),^(void){ NSLog(@"this execute in main thread") // via [NSThread isMainThread] }); 2. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){ NSLog(@"this also execute in main thread") // via [NSThread isMainThread] } 3. dispatch_queue_t queue; queue = dispatch_queue_create("com.example.MyQueue", NULL); dispatch_sync(queue, ^(void){ NSLog(@