grand-central-dispatch

How to notify a queue in Swift (GCD)

对着背影说爱祢 提交于 2019-12-20 06:39:53
问题 I'm using GCD to notify main thread (have 2 async calls inside the function) My code: func getWavesByMostRecent(closure: @escaping ([Wave]?) -> Void) { var waves = [Wave]() let dispatchGroup = DispatchGroup() self.query = DatabaseManager.waveRef.queryOrdered(byChild: Constants.reverseTimeStampKey) self.handle = self.query?.observe(.value, with: { (snapshot) in for value in snapshot.children { guard let wave = Wave(snapshot: value as! DataSnapshot) else { return } self.geoFire = GeoFire

How to send a message or notification to main thread in Xcode?

佐手、 提交于 2019-12-20 05:39:27
问题 I have a function on iPad that I need to run 3 steps in sequence, let's say task1, task2, task3. Task2 needs load some data from server. So I need to put task2 into a separate background thread. - (IBAction)dbSizeButton:(id)sender { //Task1...... dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ //Task2 .....which go to server and download some stuff and update database. dispatch_sync(dispatch_get_main_queue(), ^{ //Task3, continue

Can we update UI from background Thread?

我与影子孤独终老i 提交于 2019-12-20 04:45:25
问题 Hello iOS experts just to clear my concept I have a bit confusion about UI updation from Main Thread. Apple requirements are that all UI related stuff should be carried out in main Thread.So to test: Case1: I dispatch a task to a global dispatch concurrent queue asynchronously . After some processing I update my UI stuff directly from the concurrent queue (background thread), working fine using the below code. dispatch_queue_t myGlobalQueue; myGlobalQueue = dispatch_get_global_queue(DISPATCH

Is there any difference between dataWithContentsOfURL (threaded) and dataTaskWithURL?

試著忘記壹切 提交于 2019-12-20 04:11:18
问题 We're using dataWithContentsOfURL because it is, uh, simple... NSData *datRaw = [NSData dataWithContentsOfURL:ur]; Now, of course, that will hang the main UI thread. So we put it on another thread. We do that exactly thus, -(void)performSearch:(NSString *)stuff then:(void(^)(void))after { dispatch_queue_t otherThread =dispatch_queue_create(nil,0); dispatch_queue_t mainThread =dispatch_get_main_queue(); dispatch_async(otherThread, ^{ self.resultsRA = [self ... calls dataWithContentsOfURL ...];

GCD, NSThread, and performSelector:onThread: issues

偶尔善良 提交于 2019-12-20 03:13:45
问题 I'm attempting to debug some iOS crash logs that contain the following error message: *** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[SomeClass performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform The relevant section of the code is: - (void) runInvocationOnMyThread:(NSInvocation*)invocation { NSThread* currentThread = [NSThread currentThread]; if (currentThread != myThread) { //call over to

How to capture a variable's current value for a block

a 夏天 提交于 2019-12-20 02:56:29
问题 Is there a way to save the current value of a varible for later usage in a block? For example, for this Playground code: import UIKit import XCPlayground XCPlaygroundPage.currentPage.needsIndefiniteExecution = true class testClass { var i = 0 func test() { let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC)) * 5) dispatch_after(dispatchTime, dispatch_get_main_queue(), { self.test(self.i) }) i = 3 } func test(i: Int) { print("i: \(i)") } } let a =

Filling an array concurrently

微笑、不失礼 提交于 2019-12-20 02:00:50
问题 I am stumbling upon an issue with concurrency and arrays in Swift 5. To reproduce the issue I have simplified my code to the following fragment: import Dispatch let group = DispatchGroup() let queue = DispatchQueue( label: "Concurrent threads", qos: .userInitiated, attributes: .concurrent ) let threadCount = 4 let size = 1_000 var pixels = [SIMD3<Float>]( repeating: .init(repeating: 0), count: threadCount*size ) for thread in 0..<threadCount { queue.async(group: group) { for number in thread

UIKit and GCD thread-safety

杀马特。学长 韩版系。学妹 提交于 2019-12-19 10:29:17
问题 Many of the posts say that UIKit is totally not thread safe. Now on Apple documentation for GCD we can read that it's the DRAWING that is not thread safe. So would code like this be OK : dispatch_async( ^{ //do some work if(!self.window.rootViewController.presentedViewController && ) [self.window.rootViewController class] == anotherClass) { dispatch_async(dispatch_get_main_queue(), ^{ //do some work }); } }); 回答1: My personal opinion is that what you have heard so far is misleading. Here is a

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

走远了吗. 提交于 2019-12-19 10:04:18
问题 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

How does dispatch_set_target_queue work?

微笑、不失礼 提交于 2019-12-19 06:53:41
问题 Due to the lack of material on dispatch_set_target_queue , I have come here for help, so thanks! Here is my test code: dispatch_queue_t mySerialDispatchQueue1 = dispatch_queue_create("come.itenyh", NULL); dispatch_queue_t mySerialDispatchQueue2 = dispatch_queue_create("come.itenyh1", NULL); dispatch_set_target_queue(mySerialDispatchQueue1, mySerialDispatchQueue2); dispatch_async(mySerialDispatchQueue1, ^{[self task:@"s1"];}); dispatch_async(mySerialDispatchQueue2, ^{[self task:@"p1"];});