grand-central-dispatch

Completion block using GCD

喜夏-厌秋 提交于 2019-12-25 07:31:09
问题 I am developing an app that requires a completion block and a "barrier" functionality. As far as I know, NSOperation API has a completionBlock property and GCD API has the dispatch_barrier_(a)sync function to handle the barrier needs. What should I do in order to accomplish both of the needs? Thanks! 回答1: dispatch_group should have all the pieces you need. Just dispatch_group_async/dispatch_group_enter for your work units, then dispatch_group_notify for your completion handler. 来源: https:/

NSOperation using GCD, ensure all on the same thread

烈酒焚心 提交于 2019-12-25 06:05:11
问题 I have a 'concurrent' NSOperation , and during it's work it uses some controller classes that internally use GCD. When these controller classes return with their completion block, the completion block is on another thread. I know I could store the current thread in the operation start method and run performSelectorOnThread: , but ideally I would like to wrap the completion in a GCD block and dispatch onto the same thread as the operation started on. Is this even possible with GCD, as I can

Transforming a code to Grand Central Dispatch

被刻印的时光 ゝ 提交于 2019-12-25 05:51:00
问题 I have an array of NSNumbers that have to pass thru 20 tests. If one test fails than the array is invalid if all tests pass than the array is valid. I am trying to do it in a way that as soon as the first failure happens it stops doing the remaining tests. If a failure happens on the 3rd test then stop evaluating other tests. I am trying to convert the code I have that is serial processing, to parallel processing with grand central dispatch, but I cannot wrap my head around it. This is what I

How to prevent temporaryContext run concurrently with migratePersistentStore

北慕城南 提交于 2019-12-25 04:50:13
问题 I have a code part where I call migratePersistentStore and I want to prevent any temporaryContext to do anything in the same time, how? My idea is based on a semaphore and a dispatch_group . code A: dispatch_group_wait(dgLoadMain, DISPATCH_TIME_FOREVER) dispatch_semaphore_wait(semaLoadMain, DISPATCH_TIME_FOREVER) mainMOC!.performBlockAndWait({ mainMOC!.persistentStoreCoordinator!.migratePersistentStore(/* ... */) }) dispatch_semaphore_signal(semaLoadMain) code B: dispatch_group_enter

How to return data from background thread ?

坚强是说给别人听的谎言 提交于 2019-12-25 02:57:29
问题 I have a method that should return an array which is populated in a background thread. I would like to wait with the return statement until the array is completely populated. How would I do that ? I know how to process data with no return type, but I would like to call this function and get the populated array. Example (Here the array ends up empty because it returns before array is populated - I would like to do something to return the populated array) : -(NSArray*)fetchSomeArray{ __block

Grand Central Dispatch and functions

自古美人都是妖i 提交于 2019-12-25 02:43:35
问题 I've been looking at this question to try to solve the problem I have here. The tl;dr is I want to use GCD to let me show a "Waiting" screen while I preform some tasks, then hide the screen when it's done. Right now, I have - (void) doStuff { // Show wait on start [self.waitScreen setHidden:NO]; dispatch_queue_t queue = dispatch_queue_create("com.myDomain.myApp",null); dispatch_async(queue, ^{ dispatch_async(dispatch_get_main_queue(), ^{ // Double nesting the dispatches seems to allow me to

ARKit -[UIView setAnimationsEnabled:] Performing any operation from a background thread on UIView or a subclass is not supported

风格不统一 提交于 2019-12-25 01:46:53
问题 I have a ViewController that has a collectionView inside of it. I display the contents of the collectionView's cells inside an ARSCNView. When I set the ViewController's view property to the Material's content property I get the error from the question. material.diffuse.contents = self.myViewController.view // produces error and sometimes it crashes. When it does't crash I can see the collectionView fine but the error is still there I know it's the vc's view property that is causing the

Execute a method after the completion of a block method

点点圈 提交于 2019-12-25 00:03:45
问题 I am using RNFrostedSidebar for leftmenu. While dismissing the sidebar, below block of code is executing - (void)handleTap:(UIPanGestureRecognizer *)recognizer { [self dismissAnimated:YES completion:nil]; } - (void)dismissAnimated:(BOOL)animated completion:(void (^)(BOOL finished))completion { void (^completionBlock)(BOOL) = ^(BOOL finished){ [self rn_removeFromParentViewControllerCallingAppearanceMethods:YES]; rn_frostedMenu = nil; if (completion) { completion(finished); } }; if (animated) {

Discrepancies in time when using DispatchQueue in Swift

房东的猫 提交于 2019-12-24 21:01:09
问题 I've got a code which must be executed every half a second and I'm using the Xcode playground. I used this top answer and got a code like this: for (index, item) in array.enumerated() { DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(index), execute: { print("\(index) - \(df.string(from: Date()))" //play sound every second }) } This code is executed every second (I know that I have to divide it by 2 to get half a second but wanted to see the results). I used the DateFormatter to

MKNetworkKit and GCD dispatch_group_t

不羁的心 提交于 2019-12-24 19:46:30
问题 I am trying to use MKNetworkKit to fetch an array of links from a web service, then parse each response on a background thread, and use the dispatch_group_t of GCD to wait until all threads are finished processing. Where I'm stuck is I can't figure out why my dispatch_group_notify is not waiting for all threads in the group to complete. Running this code will print: results count: 0 added into results, count: 1 added into results, count: 2 The dispatch group is not waiting on its threads. I