grand-central-dispatch

Risk Assessment: Using Pthreads (vs. GCD or NSThread)

一世执手 提交于 2019-12-09 11:31:06
问题 A colleague suggested recently that I use pthreads instead of GCD because it's, "way faster." I don't disagree that it's faster, but what's the risk with pthreads? My feeling is that they will ultimately not be anywhere nearly as idiot-proof as GCD (and my team of one is 50% idiots). Are pthreads hard to get right? 回答1: GCD and pthreads are both ways of doing work asynchronously, but they are significantly different. Most descriptions of GCD describe it in terms of threads and of thread

How do I watch for file changes on OS X?

对着背影说爱祢 提交于 2019-12-09 07:08:33
问题 I would like to be notified of writes into a given file – without polling, without having to read from the file and without having to monitor the parent directory and watch the file modification time stamps. How do I do that? 回答1: I couldn’t find a simple example, so I’m contributing what I came up with for future reference: @interface FileWatch () @property(assign) dispatch_source_t source; @end @implementation FileWatch @synthesize source; - (id) initWithPath: (NSString*) path targetQueue:

Write macros for GCD calls?

为君一笑 提交于 2019-12-09 06:47:53
问题 I'd like to create a macro for GCD calls like for example: dispatch_async(dispatch_get_main_queue(), ^{ stuff.... }); the macro could look something like this: main(^{...})? Not sure how to write it. Any suggestion? thank you 回答1: You can define macros like this: #define ASYNC(...) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ __VA_ARGS__ }) #define ASYNC_MAIN(...) dispatch_async(dispatch_get_main_queue(), ^{ __VA_ARGS__ }) First one will invoke the code

Check if on correct dispatch queue in Swift 3

点点圈 提交于 2019-12-09 04:36:15
问题 I have a few unit tests in which I'd like to test if a callback is called on the correct dispatch queue. In Swift 2, I compared the label of the current queue to my test queue. However in Swift 3 the DISPATCH_CURRENT_QUEUE_LABEL constant no longer exists. I did find the dispatch_assert_queue function. Which seems to be what I need, but I'm not sure how to call it. My Swift 2 code: let testQueueLabel = "com.example.my-test-queue" let testQueue = dispatch_queue_create(testQueueLabel, nil) let

Realm accessed from incorrect thread - Swift 3

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 03:16:51
问题 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? 回答1:

iphone - is it ok to use usleep on a secondary thread on Grand Central Dispatch?

。_饼干妹妹 提交于 2019-12-09 02:09:29
问题 I am adding a block to a queue (not the main queue), using Grand Central Dispatch. This block has a loop and between every passage of the loop a small delay of 0,02 seconds is required. I am using dispatch_async(myOwnQueue, ^{ // begin loop // do stuff usleep(20000); // end loop }); on this queue. As it is not the main queue, it will not block the main thread. The problem is that Xcode complains: warning: implicit declaration of function 'usleep' Is there other way to do that? thanks. 回答1:

Cancel GCD async task when navigating back

和自甴很熟 提交于 2019-12-09 01:02:19
问题 I have a relatively long running task (5-10 secs) in a view controller in my iOS app. It is running async in the background by GCD. The user has the ability to make UI operations during this task, so he also has the ability to press back button to navigate to the previous view. My code is pretty straightforward: dispatch_queue_t queue = dispatch_queue_create("com.x.y.z", NULL); dispatch_async(queue, ^{ TestObject* test = [self getTestObject]; dispatch_async(dispatch_get_main_queue(), ^{ test

Type 'DispatchQueue.Attributes' has no member 'serial'

 ̄綄美尐妖づ 提交于 2019-12-08 16:00:33
问题 I have converted existing Swift2.3 code to Swift3.0 using Xcode8 beta4. Xcode automatically convert syntax to Swift3.0, but it not able to create serial dispatch queue. private let serialQueue = DispatchQueue(label: "identifier", qos: DispatchQueue.Attributes.serial) 回答1: There is not .serial attribute anymore, but dispatch queues are by default serial, unless you specify the .concurrent attribute: let serialQueue = DispatchQueue(label: "label") let concurrentQueue = DispatchQueue(label:

Objective-C – Structuring GCD code for background processing

微笑、不失礼 提交于 2019-12-08 13:03:29
I have some code that takes a little time to process and thus appropriately it should not run on the main queue. However I'm not sure on how to correctly "structure" the GCD code segments. I.e every time the app becomes active I'm doing a syncing operation: AppDelegate.m - (void)applicationDidBecomeActive:(UIApplication *)application { AddressBookHelper *abHelper = [AddressBookHelper sharedInstance]; // singleton helper class of NSObject [abHelper sync]; } The syncing code inside AddressBookHelper looks something like this: AddressBookHelper.m - (void)sync { NSArray *people = // Fetching some

If dispatch_sync does not use a different thread, can it be faster?

纵然是瞬间 提交于 2019-12-08 13:02:59
问题 I'm running this code dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSLog(@"Main Thread? %d", [NSThread isMainThread]); // non-trivial non-UI code here }); The NSLog indicates that I'm actually on the main thread, but... my impression is that the non-trivial non-UI code runs much faster if I use dispatch_sync (as opposed to nothing). If it is running on the main thread, is there any way it could be faster? 回答1: The global concurrent GCD queues have no ordering,