grand-central-dispatch

Multi-threaded Objective-C accessors: GCD vs locks

会有一股神秘感。 提交于 2019-12-14 03:39:41
问题 I'm debating whether or not to move to a GCD-based pattern for multi-threaded accessors. I've been using custom lock-based synchronization in accessors for years, but I've found some info (Intro to GCD) and there seems to be pros of a GCD-based approach. I'm hoping to start a dialog here to help myself and others weigh the decision. The pattern looks like: - (id)something { __block id localSomething; dispatch_sync(queue, ^{ localSomething = [something retain]; }); return [localSomething

Enforcing one-at-a-time access to pointer from a primative wrapper

风格不统一 提交于 2019-12-14 03:19:43
问题 I've read a fair amount on thread-safety, and have been using GCD to keep the math-heavy code off the main thread for a while now (I learned about it before NSOperation, and it seems to still be the easier option). However, I wonder if I could improve part of my code that currently uses a lock. I have an Objective-C++ class that is a wrapper for a c++ vector. (Reasons: primitive floats are added constantly without knowing a limit beforehand, the container must be contiguous, and the reason

Looping dispatch_after in a method causes many simultaneous dispatches when method is rerun

倖福魔咒の 提交于 2019-12-14 01:07:04
问题 I'm creating a simple game. I have the following code: - (void)doStuff { double delayInSeconds = [NSNumber randomFloatBetweenLowerBound:0.8f upperBound:2.6f]; // Own category on NSNumber returns random float. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // // Do stuff // if ([self shouldDoMoreStuff]) { [self doStuff]; } }); } This method is run in viewDidLoad , but it is also

DispatchGroup not working in Swift 3

限于喜欢 提交于 2019-12-13 23:05:24
问题 I am trying to make dispatchgroup work in my code let dispatchQueue:DispatchQueue = DispatchQueue(label: "com.dispatchgroup", attributes: .concurrent, target: .main) var dispatchGroup:DispatchGroup = DispatchGroup() func renewLoginIfRequired()->String{ self.dispatchGroup.enter() dispatchQueue.async(group:dispatchGroup){ self.authorizeApplication(completionHandler: { self.dispatchGroup.leave() }) } } self.dispatchGroup.wait() // Stops execution here return "Login Success" } Above code stops

What exactly is GCD and where should it be used?

余生长醉 提交于 2019-12-13 19:28:59
问题 I think GCD is todo with asynchronous events, but specifically, I am not sure what type of events/where GCD comes in useful, apart from the obvious web requests. Could you explain what GCD is and where it should/shouldn't be used? 回答1: I strongly suggest you to read the chapter called "Dispatch Queues" in Apple's Concurrency Programming Guide. It is an excellent document and clearly depicts the differences with standard multi-thread programming. This reading (will not take you more than 10

DispatchQueue.main.sync returning exc_bad_instruction Swift 3

℡╲_俬逩灬. 提交于 2019-12-13 12:53:38
问题 I want to display an ActivityIndicatorView in my app, but when I call the sync method from the main thread, the app crashes with the error: exc_bad_instruction (code=exc_i386_invop subcode=0x0) I'm using xcode 8.0 and swift 3 Can someone please help me? func POST(endpoint:NSString!,body:NSString!,vc:UIViewController? = nil)->NetworkResult{ let result = NetworkResult() DispatchQueue.main.sync { self.displayActivityIndicator(viewController: vc) } let urlStr = self.url.appending(endpoint as

Waiting for condition to continue

别说谁变了你拦得住时间么 提交于 2019-12-13 12:49:11
问题 I have a method that I add to a GCD queue that I have created (so it's a serial queue) and then run it async. From within that block of code I make a dispatch to the main queue, when that block of code dispatched to the main queue is complete I set a BOOL flag to YES, so that I further down in my code can check if this condition is YES then I can continue to the next method. Here is the code in short: dispatch_queue_t queue = dispatch_queue_create("ProcessSerialQueue", 0); dispatch_async

UILabel won't update even on dispatch_async

て烟熏妆下的殇ゞ 提交于 2019-12-13 08:36:09
问题 Any reason why my label won't update? - (void)setLabel:(NSNotification*)notification { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"setLabel"); [self.label setText:@"My Label"]; }); } I've also tried using performSelectorOnMainThread to no avail. Note that setLabel is appears on the log. Additional info: I also have two other functions which does the same thing but only with a different text. The two other functions doesn't have dispatch_async but they both work. Also, the

Swift: Return boolean in GCD Completion Block

巧了我就是萌 提交于 2019-12-13 07:35:19
问题 I have a function written in Swift. I want the completion block to return a boolean. How can I go about doing this? I am using Grand Central Dispatch. func myFunc() -> Bool { var success:Bool = false // code here dispatch_async(dispatch_get_main_queue(), { return success )} )} } thanks! 回答1: Standard why of dealing with this async nature is not to return value, but pass in completion handler: func myFunc(completion:(success: Bool) -> ()) { var success:Bool = false // code here dispatch_async

Efficiently sync data in iOS

最后都变了- 提交于 2019-12-13 04:48:21
问题 I am working on app where I sync data to and from the server, when app enters background and app becomes active. Because as I sync to multiple devices, the data should be updated on all devices. So here is how I do it, - (void)applicationDidBecomeActive:(UIApplication *)application { if([[AppManager instance].globalManager activeSyncCounter] == 0) { [_webService callServerAPI]; } } - (void)applicationDidEnterBackground:(UIApplication *)application { if([[AppManager instance].globalManager