grand-central-dispatch

Concurrent Queue with GCD? (iOS 4.2.1)

纵然是瞬间 提交于 2019-12-01 14:55:35
问题 Iam having problems with: dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0UL); concurrentQueue is nil on iOS 4.2.1 (device) but the same code works perfectly on another device which is running iOS 5.0.1. When I checked the header it says it's available since iOS 4.0, am I doing something wrong? The code below fetches images from the internet and works great in everything after 4.2.1 but not in 4.2.1, any ideas why? Can you create a concurrent

How to properly declare a computed property, when calculation uses background threads?

三世轮回 提交于 2019-12-01 12:02:43
问题 I'm trying to declare a computed property that consists of a block, executed in the background thread. So, when I address this property, it's nil, as the computation returns the result when it is not ready. How to better correct this? Thank you! enum Result<T> { case error(error: Error) case success(data: T) } var userID: Result<CKRecordID>? { var result: Result<CKRecordID>? = nil container.fetchUserRecordID { recordID, error in if let error = error { result = .error(error: error) } if let

Using Grand Central Dispatch, how can I check if there is a block already running?

我是研究僧i 提交于 2019-12-01 11:31:46
I'm using GCD to do some background loading from the internet. This works great except for a little flaw. In my app I have 3 tabs and when clicking on any tab the GCD starts to do the background loading for the appropriate tab. If the user decides to go from the first tab to the second tab (when the GCD has started downloaded data for the first tab) and then returns to the first tab again. GCD will start another background thread (even though the first background thread hasn't finished downloaded the data yet). So is there a way to check if a background thread is currently running? So that it

How to dispatch_group_wait for dispatch_group_async inside an asynchronous block

六眼飞鱼酱① 提交于 2019-12-01 08:57:05
I have code that looks something like this: [SVProgressHUD show]; [imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, ...) { dispatch_group_async(queueGroup, queue, ^{ // Do stuff }); }]; dispatch_group_wait(queueGroup, DISPATCH_TIME_FOREVER); [SVProgressHUD dismiss]; Basically, display a loading animation HUD and start generating image thumbnails from an asset, then once it's done hide the HUD. I'm using a dispatch group since i want to make sure all the thumbnails are generated before i hide the HUD. But when i run it, the HUD gets dismissed

Use queue and semaphore for concurrency and property wrapper?

不想你离开。 提交于 2019-12-01 08:34:05
问题 I'm trying to create a thread-safe property wrapper. I could only think of GCD queues and semaphores as being the most Swifty and reliable way. Are semaphore's just more performant (if that's true), or is there another reason to use one over the other for concurrency? Below are two variants of atomic property wrappers: @propertyWrapper struct Atomic<Value> { private var value: Value private let queue = DispatchQueue(label: "Atomic serial queue") var wrappedValue: Value { get { queue.sync {

Realm accessed from incorrect thread - Swift 3

孤人 提交于 2019-12-01 07:33:09
At the top of my UITableViewController is the following: let queue = DispatchQueue(label: "background") When a task is deleted, the following executes: self.queue.async { autoreleasepool { let realm = try! Realm() realm.beginWrite() realm.delete(task) do { try realm.commitWrite() } catch let error { self.presentError() } } } And then I receive the error terminating with uncaught exception of type realm::IncorrectThreadException: Realm accessed from incorrect thread. How could I fix this? Samantha It seems like the write is happening on a different thread than the object was originally accessed

Issue with GCD and too many threads

无人久伴 提交于 2019-12-01 05:57:30
I have an image loader class which provided with NSURL loads and image from the web and executes completion block. Code is actually quite simple - (void)downloadImageWithURL:(NSString *)URLString completion:(BELoadImageCompletionBlock)completion { dispatch_async(_queue, ^{ // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ UIImage *image = nil; NSURL *URL = [NSURL URLWithString:URLString]; if (URL) { image = [UIImage imageWithData:[NSData dataWithContentsOfURL:URL]]; } dispatch_async(dispatch_get_main_queue(), ^{ completion(image, URLString); }); }); } When I

GCD dispatch concurrent queue freeze with 'Dispatch Thread Soft Limit Reached: 64' in crash log

笑着哭i 提交于 2019-12-01 04:58:05
My program is a server which handles incoming requests. Each valid request is wrapped in NSOperation and passed to a normal NSOperationQueue . Each NSOpearation processes its request. In some cases, there is contention at a NSDictionary which I use dispatch_queue (concurrent queue), dispatch_barrier_async (when set value) and dispatch_sync (when get value) to make this NSDictionary thread-safe. I test my program with 100 requests concurrently then the process freezes sometimes. I kill the process with SIGSEGV to see crash log. Most of the threads stuck at dispatch_sync of this queue. And there

Issue with GCD and too many threads

好久不见. 提交于 2019-12-01 03:53:22
问题 I have an image loader class which provided with NSURL loads and image from the web and executes completion block. Code is actually quite simple - (void)downloadImageWithURL:(NSString *)URLString completion:(BELoadImageCompletionBlock)completion { dispatch_async(_queue, ^{ // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ UIImage *image = nil; NSURL *URL = [NSURL URLWithString:URLString]; if (URL) { image = [UIImage imageWithData:[NSData dataWithContentsOfURL

How to convert dispatch_data_t to NSData?

霸气de小男生 提交于 2019-12-01 03:48:32
Is this the right way? // convert const void *buffer = NULL; size_t size = 0; dispatch_data_t new_data_file = dispatch_data_create_map(data, &buffer, &size); if(new_data_file){ /* to avoid warning really - since dispatch_data_create_map demands we care about the return arg */} NSData *nsdata = [[NSData alloc] initWithBytes:buffer length:size]; // use the nsdata... code removed for general purpose // clean up [nsdata release]; free(buffer); // warning: passing const void * to parameter of type void * It is working fine. My main concern is memory leaks. Leaking data buffers is not fun. So is the