grand-central-dispatch

iOS Multithreading - NSURLSession and UI updates

末鹿安然 提交于 2019-12-06 04:24:04
问题 I have a general question about multithreading in iOS: In my very simple test app, I use NSURLSession to download some small images from the server and present them in a table view. Within the callback of the NSURLSession, after retrieving the images, I call tableview.reloadData() like so: var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url)) {(data, response, error) -> Void in /* Process images here */ self.tableView.reloadData() } session.resume() The images download almost

How to sync serial queue for URLSession tasks?

ⅰ亾dé卋堺 提交于 2019-12-06 03:42:54
问题 Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1, I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request). Since my first dataTask delivers a result that is needed in the httpBody of the second dataTask, the two URLSession.shared.dataTasks shall run in series, one after the other! (and also the preparative code shall run consecutively). I tried, so far, using two consecutive serialQueue.sync{} queues. But I had to realize

global_queue with qos_class_user_interactive

人走茶凉 提交于 2019-12-06 01:42:00
I try to understand GCD and wrote this code to find out run priority: override func viewDidLoad() { super.viewDidLoad() fetchImage() print(1) dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { print(2) } dispatch_async(dispatch_get_main_queue()) { print(3) } dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) { print(5) } } I got next result in the console: 1 2 5 3 So the question is: Part 1 : Why 3 is after 5 (main_queue has highest priority?) Part 2 : And why 2 is higher that 3 and 5 as well? Thank you guys! Bear in mind: this is multi-threading, on

Nested Dispatch Groups Swift

流过昼夜 提交于 2019-12-06 00:46:39
问题 When a user creates a new group in my app I have to push invites to the database as well as other information. I've started using Dispatch Groups in order to keep track of when all the information is successfully sent out so I can dismiss the view. I'm trying to use a dispatch group for the invites and another dispatch group for all the data. Here's what I have: // Push new data to db func createGroup(onSccess completion:@escaping () -> Void) { let inviteDispatchGroup = DispatchGroup() let

What is the purpose of Semaphore.wait(timeout: .now())?

☆樱花仙子☆ 提交于 2019-12-05 21:45:16
Looking at some Apple code sample, I found this: func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { // wait() is used to drop new notifications if old ones are still processing, to avoid queueing up a bunch of stale data. if metadataObjectsOverlayLayersDrawingSemaphore.wait(timeout: .now()) == .success { DispatchQueue.main.async { // Some processing... self.metadataObjectsOverlayLayersDrawingSemaphore.signal() } } } Context of the code : This is the delegate method when using video capture to detect a QR

dispatch_once call causes crash

徘徊边缘 提交于 2019-12-05 20:54:25
dispatch_once call causes crash (in simulator) after I've converted my project to ARC. My original problem was that I've got EXC_BAD_ACCESS (in objc_retain call) crash in one of my singleton object's + (SingletonClass)shared { ... dispatch_once(..., ^{}); ... } method exactly one line before the dispatch_once call. Based on loggings, and breakpoints my code have not run into the dispatch_once call's block. I didn't know the reason, so I've just commented out the dispatch_once call. My app haven't crashed without that call. After that I've tried to put dispatch_once in a method that my app

calling performSelectorInBackground: from background thread

瘦欲@ 提交于 2019-12-05 20:17:40
What is the real effect of calling performSelectorInBackground:... from a method that is running in the background? I want it to run asynchronously For example: - (void) _imageBufferWasUpdated{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //do something here if(shouldContinue){ [self performSelectorInBackground:@selector(_loop) withObject:nil]; } [pool release]; } _imageBufferWasUpdated will run in the background and I want to call _loop method asynchronously (in the background also so _imageBufferWasUpdated will finish soon, probably before _loop ends). Is this correct? Is

How do I repeatedly loop dispatch_after statements?

时光毁灭记忆、已成空白 提交于 2019-12-05 20:17:21
I want to have a for loop with dispatch_after statements in them. Problem is that the dispatch_after calls don't seem to go in line with the for loop. In other words, I want it to only start the next iteration of the for loop after the statements in the dispatch_after block have executed. How would I do this? Use Case I want to present words on screen. Traditionally I show one word per second. But depending on the word length, I now want to show longer words for slightly longer, and shorter words for slightly less time. I want to present a word, wait a little while (depending on how long the

Why NSURLConnection delegate methods don't get called, when using the global dispatch queue?

断了今生、忘了曾经 提交于 2019-12-05 19:44:02
When I do the following: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL), ^{ create NSURLRequest; create NSURLConnectionDelegate; create NSURLConnection; start NSURLConnection; }); The delegate's methods never get called. But when I do dispatch_async(dispatch_get_main_queue(), ^{ create NSURLRequest; create NSURLConnectionDelegate; create NSURLConnection; start NSURLConnection; }); They do get called. Why? UPD http://developer.apple.com/library/ios/#qa/qa1712/_index.html Now I do create NSURLConnection; start NSURLConnection; on the main thread. In the first case,

dispatch_async in NSManagedObjectContext's performBlock

半世苍凉 提交于 2019-12-05 19:03:32
According to the WWDC 2012 video, "Core Data Best Practices", dispatch_sync should be used to run some kind of callback in performBlock of context, which is created as a type of NSPrivateQueueConcurrencyType . Why is that? Can I use dispatch_async(dispatch_get_main_queue(), 0) ... to call some UI-related callbacks in private queue's context's performBlock ? No. NSPrivateQueueConcurrencyType manages it's own internal queue, and does not enjoy you trying to leave one of it's threads to do what you want to do (in fact, I believe it throws exceptions when this type of behavior occurs). There's a