grand-central-dispatch

dispatch_sync vs. dispatch_async on main queue

随声附和 提交于 2019-12-02 14:06:30
Bear with me, this is going to take some explaining. I have a function that looks like the one below. Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that contains instances of another Core Data entity called LPFile. Each LPFile represents a file on disk and what we want to do is open each of those files and parse its text, looking for @import statements that point to OTHER files. If we find @import statements, we want to locate the file they point to and then 'link' that file to this one by adding a relationship to the core data entity that

Passing an argument to dispatch_async

你离开我真会死。 提交于 2019-12-02 13:47:39
问题 I'm new to Swift and looking at how the dispatch_async function works. The API doc shows dispatch_async having two parameters. However, I'm able to pass in one argument and it's okay. dispatch_async(dispatch_get_main_queue()) { } How come I don't need to pass in two arguments? Thank you, API Doc: 回答1: It is a trailing closure syntax func someFunctionThatTakesAClosure(closure: () -> ()) { // function body goes here } // here's how you call this function without using a trailing closure:

Use Grand Central Dispatch to schedule a function same time every day

喜你入骨 提交于 2019-12-02 11:47:52
问题 I have read this answer and I do not believe it has what I am looking for but I a am beginner and I am happy to have someone point out the answer in this link : dispatch_after - GCD in swift? My goal: set a function to run at 9 AM in the user's time zone (or system's time zone) every single day. I've used GCD very briefly to delay a function as follows (it works perfectly fine): var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC))) dispatch

DispatchWorkItem not notifying main thread

ぐ巨炮叔叔 提交于 2019-12-02 11:14:09
Note: This is not duplicate question I have already seen Dispatch group - cannot notify to main thread There is nothing answered about DispatchWorkItem I have code like below let dwi3 = DispatchWorkItem { print("start DispatchWorkItem \(Thread.isMainThread)") sleep(2) print("end DispatchWorkItem") } let myDq = DispatchQueue(label: "A custom dispatch queue") dwi3.notify(queue: myDq) { print("notify") } DispatchQueue.global().async(execute: dwi3) Which is working correctly (I can see notify on console) and not in main thread start DispatchWorkItem false start DispatchWorkItem false end

In Swift, how to stop all the process until datas retrieved from parse.com in UICOLLECTIONVIEW

让人想犯罪 __ 提交于 2019-12-02 09:33:44
In CollectionView I am displaying datas from parse.com. Successfully retrieved. But unable to display in the cell. I am receiving error as Array outbound. I found out the mistake, parse is running as asynchronous. But, before parse end, collection view gets loaded. So I unable to display values in the cell. It is throwing an error. How to stop all the process until parse get loaded completely? Kindly guide me. MY CODING IS BELOW: //VIEWCONTROLLER having collectionView var myList : NSArray = NSArray() let obj_par = parse_retrive() obj_par.parse_DB() //DATAS GOING TO RETRIVE FROM PARSE story

for loop cycle showing always the last array value swift 3 after HTTP request

心不动则不痛 提交于 2019-12-02 09:19:24
问题 I already used another post to solver part of the problem,so now I am able to wait the end of the request. I made a http request with Almofire and parse the resulting JSON file. the code inside the myGroup.notify i think is correctly executed ater the request is completed (if I print allCards.count returns the correct value). Why if I put the for loop inside it is always showing the last card? Thanks for any help import UIKit mport SwiftyJSON import Alamofire class ViewController:

GCD : How to write and read to variable from two threads

非 Y 不嫁゛ 提交于 2019-12-02 08:41:29
问题 this may sound a newbie question anyway Im new to GCD, I'm creating and running these two following threads. The first one puts data into ivar mMutableArray and the second one reads from it. How do i lock and unclock the threads to avoid crashes and keep the code thread safe ? // Thread for writing data into mutable array dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,

How do I use DispatchGroup / GCD to execute functions sequentially in swift?

♀尐吖头ヾ 提交于 2019-12-02 07:44:26
I need to run two functions sequentially before I refresh my collection view. The first function pulls autoids from firebase and writes them into an array. The second function (getLocation) then uses that array to make another call to firebase to retrieve a value (location) beneath each autoid. I'm using DispatchGroup() to ensure the first function finishes before the second one begins. But I also need the second function to complete before the notify refreshes the collection view. I've written a basic for loop to test the DispatchGroup() in the second function and was able to get it to

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

早过忘川 提交于 2019-12-02 07:19:08
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 ...]; dispatch_async(mainThread, ^{ if (after) after(); }); }); } (Incidentally, here's an excellent

dispatch_after is limited to 10 seconds?

五迷三道 提交于 2019-12-02 06:54:05
问题 I'm developing an app which is running in background. Sometimes I need to tell the user that something is happenning so I play a sound a certain number of times. To do that I made a timer but the problem is that It cannot exceed 10s of the total delayed time, after that, there are no more sounds (but the app is still running). I've checked this behavior in foreground mode and it works perfectly. This is my code when I need to inform the user: AudioServicesPlaySystemSound(alarma);