grand-central-dispatch

Serializing NSURLConnection Requests (iOS) - Use Synchronous Request?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 15:38:03
问题 I'm looping through a list of dates and making a request to a web server for each date in the list. I would like each date to be processed completely before the subsequent request is sent to the server. To do this, I have set up a serial dispatch queue using GCD. Each time through the date loop, a block is added to the queue. The problem I am having is that my NSURLConnection is set up using the standard asynchronous call. This results in requests not blocking any subsequent requests. They

CollectionViewCell image doesn't display until scrolling

老子叫甜甜 提交于 2019-12-11 14:55:15
问题 I am using this code to display image in to my customCell. This is my code to fetch image from API. It was working fine as per the given link. But the problem started when I added this code to my cellForItemAt indexPath: for image Cache. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // Displaying other elements with text customCell.mainImage.image = nil var image1 = self.imageCache.object(forKey: indexPath.item as

Thread-safe access to a datasource during a tableView update

我们两清 提交于 2019-12-11 13:37:16
问题 My app uses a tableView with a data source that can be updated asynchronously by multiple threads. When the data source is changed, the tableView is updated, not reloaded, using tableView.performBatchUpdates({ tableView.deleteRows(at: deletions, with: .automatic) tableView.insertRows(at: insertions, with: .automatic) for (from, to) in movements { tableView.moveRow(at: from, to: to) } }, completion: { (finished) in if !finished { self.tableView.reloadData() } else { // some cleanup code }

Can't get my Timer in Swift to fire in Playground

匆匆过客 提交于 2019-12-11 11:58:17
问题 I'm trying to make a nice timer using Swift and GCD. I find lots of blogs and even an entry in Matt Neuburg's book. I used the latter to put together my own variant in a playground: import Foundation struct Timer { private var queue = dispatch_queue_create("timer", nil) private var source: dispatch_source_t var tick:()->() = {} { didSet { self.update() } } var rate:Double = 1.0 { didSet { self.update() } } init() { self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self

GCD with nested Parse Queries

眉间皱痕 提交于 2019-12-11 10:58:17
问题 func getPosts(skip: Int){ var query = PFQuery(className: self.parseClassName!) query.includeKey("posted_by") query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in if error == nil && objects != nil { if let objects = objects as? [PFObject] { var requestGroup = dispatch_group_create() for post in objects { dispatch_group_enter(requestGroup) let queryKommentar1 = PFQuery(className:"Comment") queryKommentar1.whereKey("posted_to", equalTo: post)

How to make a queue of NSURLRequest using GCD?

浪尽此生 提交于 2019-12-11 10:39:58
问题 I have a NSMutableArray containing 7 internet URLs from which I need to grab the HTTP headers. I'm using these methods to make asynchronous connections (and all works perfectly): - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error - (void)connectionDidFinishLoading:(NSURLConnection *

dispatch_after time triggers immediately

时间秒杀一切 提交于 2019-12-11 08:34:31
问题 This is the first time I've used GCD, I'll admit, so sorry if I've been stupid. I have a dispatch_after command which acts as a handy delay for me. My problem is that when I send dispatch_after(500000000000, dispatch_get_main_queue()){ println("triggered") //or any other code } the closure is triggered immediately (e.g. I have tested this and "triggered" prints immediately). It should take longer right? Like 500 seconds longer. Thanks :) 回答1: The first parameter of dispatch_after(_:_:_:) is

Forcing the order of execution using dispatch_sync

半腔热情 提交于 2019-12-11 07:58:55
问题 My library exposes 2 APIs as follows to: -(void) createFile{ dispatch_sync(queueSerial, ^{ //B1 [fileObj createFileInfo:file completion:^(NSError *error){ //execute completion block C1 }]; }); } -(void) readFile:(NSData*)timeStamp{ dispatch_async(queueSerial, ^{ //B2 [fileObj readFileInfo:fileName completion:^(NSError *error) { dispatch_async(queueSerial2, ^{ //execute completion block C2 }); }] }); } Both readFile and createFile are asynchronous methods. I usually recommend to the people

Why does dispatch_semaphore_wait() return YES all the time even when I'm not scrolling?

眉间皱痕 提交于 2019-12-11 07:55:57
问题 Brad Larson delivered a solution for the CADisplayLink freeze issue when scroll views are scrolling. My OpenGL ES draw method is called by a CADisplayLink , and I tried Brad's technique but can't make it work. The core problem is that my OpenGL ES view is hosted by a UIScrollView , and when the U IScrollView scrolls, the CADisplayLink stops firing. The technique Brad described is supposed to let the CADisplayLink continue to fire even during scrolling (by adding it to NSRunLoopCommonModes

DispatchGroup in For Loop

十年热恋 提交于 2019-12-11 07:25:50
问题 So, I'm having a bit of a time trying to get DispatchGroup to keep a for loop from iterating before a long asynchronous operation has completed. Most examples I've found are fairly straight forward and clear, but I can't seem to get my simple test case to work as I would expect. let group = DispatchGroup() for i in 1...3 { group.enter() print("INDEX \(i)") asynchronousOperation(index: i, completion: { print("HELLO \(i)") self.group.leave() }) print("OUTSIDE \(i)") } func asynchronousOperation