grand-central-dispatch

Asynchronous swift 3

梦想与她 提交于 2020-01-15 09:20:06
问题 I need to make an asynchronous call so that the second method is only called after the first one is completed.Both methods are network calls. Something like this: signIn() getContacts() I want to make sure that getContacts only gets called after the signIn is completed. FWIW, I can't edit the methods signatures because they are from a Google SDK. This is what I tried: let queue = DispatchQueue(label: "com.app.queue") queue.async { signIn() getContacts() } 回答1: Async calls, by their nature, do

GCD vs custom queue

不羁岁月 提交于 2020-01-15 05:54:26
问题 I was wondering what is the difference in performance between these two. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // perform complex operation // dispatch back to main thread to update UI }); dispatch_async(_myCustomConcurrentQueue, ^{ // perform complex operation // dispatch back to main thread to update UI }); My assumption is the GCD is used across the os and other applications, and it will need to perform very quick background tasks, and be finished

Asynchronous Swift processing in Linux not working

大憨熊 提交于 2020-01-15 05:24:09
问题 I'm trying to understand how Swift 4.0 asynchronous processing works in Linux. After looking at the documentation and some answers on SO I came up with this simple example: import Dispatch import Glibc DispatchQueue.main.asyncAfter(deadline: .now()) { print("Done!") } print("Sleeping for 2 seconds...") usleep(2 * 1_000_000) print("Exiting...") However, this only prints: Sleeping for 2 seconds... Exiting... Why does it not print Done! ? What am I missing? How do I write a simple parallel

How to handle Race Condition Read Write in Swift?

最后都变了- 提交于 2020-01-15 01:49:04
问题 I have got a concurrent queue with dispatch barrier from Raywenderlich post Example private let concurrentPhotoQueue = DispatchQueue(label: "com.raywenderlich.GooglyPuff.photoQueue", attributes: .concurrent) Where write operations is done in func addPhoto(_ photo: Photo) { concurrentPhotoQueue.async(flags: .barrier) { [weak self] in // 1 guard let self = self else { return } // 2 self.unsafePhotos.append(photo) // 3 DispatchQueue.main.async { [weak self] in self?.postContentAddedNotification(

How to handle Race Condition Read Write in Swift?

独自空忆成欢 提交于 2020-01-15 01:48:33
问题 I have got a concurrent queue with dispatch barrier from Raywenderlich post Example private let concurrentPhotoQueue = DispatchQueue(label: "com.raywenderlich.GooglyPuff.photoQueue", attributes: .concurrent) Where write operations is done in func addPhoto(_ photo: Photo) { concurrentPhotoQueue.async(flags: .barrier) { [weak self] in // 1 guard let self = self else { return } // 2 self.unsafePhotos.append(photo) // 3 DispatchQueue.main.async { [weak self] in self?.postContentAddedNotification(

Modifying struct instance variables within a Dispatch closure in Swift

老子叫甜甜 提交于 2020-01-14 07:24:21
问题 I'm using the DEVELOPMENT-SNAPSHOT-2016-06-06-a version of Swift. I cannot seem to get around this issue, I've tried using @noescape in various places, but I still have the following error: Closure cannot implicitly capture a mutating self parameter To better explain, here is a simple example: public struct ExampleStruct { let connectQueue = dispatch_queue_create("connectQueue", nil) var test = 10 mutating func example() { if let connectQueue = self.connectQueue { dispatch_sync(connectQueue)

Modifying struct instance variables within a Dispatch closure in Swift

只愿长相守 提交于 2020-01-14 07:24:20
问题 I'm using the DEVELOPMENT-SNAPSHOT-2016-06-06-a version of Swift. I cannot seem to get around this issue, I've tried using @noescape in various places, but I still have the following error: Closure cannot implicitly capture a mutating self parameter To better explain, here is a simple example: public struct ExampleStruct { let connectQueue = dispatch_queue_create("connectQueue", nil) var test = 10 mutating func example() { if let connectQueue = self.connectQueue { dispatch_sync(connectQueue)

CoreData not compatible with an AFNetworking request from a GCD queue?

本小妞迷上赌 提交于 2020-01-14 05:16:29
问题 I am using GCD to start a long-running background process ('run_loop') that creates an NSManagedObjectContext ('MOC'), monitors CoreData objects, and sometimes (when they're ready) uploads a serialization of them to a webserver and then deletes them. I am using AFNetworking for the HTTP calls. The problem is in the request completion handler blocks, as the blocks run in a different thread to the owner of the MOC, which isn't supported by CoreData. I have tried storing the NSThread from the

How to stop DispatchGroup or OperationQueue waiting?

依然范特西╮ 提交于 2020-01-14 03:32:24
问题 DispatchGroup and OperationQueue have methods wait() and waitUntilAllOperationsAreFinished() which wait for all operations in respective queues to complete. But even when I call cancelAllOperations it just changes the flag isCancelled in every running operation and stop the queue from executing new operations. But it still waits for the operations to complete. Therefore running the operations must be stopped from the inside. But it is possible only if operation is incremental or has an inner

Convert Data to DispatchData in Swift 4

感情迁移 提交于 2020-01-13 19:51:14
问题 I am migrating a project to Swift 4 and I cannot figure out how I am supposed to use the new API:s to do this in Swift 4. The following code is the old Swift 3 way (from the middle of a function hence the guard): let formattedString = "A string" guard let stringData: Data = formattedString.data(using: .utf8) else { return } let data: DispatchData = [UInt8](stringData).withUnsafeBufferPointer { (bufferPointer) in return DispatchData(bytes: bufferPointer) } Now it gives the following warning: