swift: async task + completion - ios

I got quite a lot of confusion on async tasks in swift. What i want to do is something like this...
func buttonPressed(button: UIButton) {
// display an "animation" tell the user that it is calculating (do not want to freeze the screen
// do some calculations (take very long time) at the background
// the calculations result are needed to update the UI
}
I tried to do something like this:
func buttonPressed(button: UIButton) {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) { () -> Void in
// display the animation of "updating"
// do the math here
dispatch_async(dispatch_get_main_queue(), {
// update the UI
}
}
}
However, I found that the UI is updated without waiting my calculation to be completed. I am quite confused on the use of async queue. Anyone help? Thanks.

You need a function with a asynchronous completion handler.
At the end of the calculation call completion()
func doLongCalculation(completion: () -> ())
{
// do something which takes a long time
completion()
}
In the buttonPressed function dispatch the calculate function on a background thread and after completion back to the main thread to update the UI
func buttonPressed(button: UIButton) {
// display the animation of "updating"
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
self.doLongCalculation {
dispatch_async(dispatch_get_main_queue()) {
// update the UI
print("completed")
}
}
}
}

dispatch_queue_t dispatchqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchqueue, ^(void){
while ([self calculate]) {
NSLog(#"calculation finished");
dispatch_async(dispatch_get_main_queue(), ^{
// update the UI
});
}
});
- (BOOL)calculate
{
//do calculation
//return true or false based on calculation success or failure
return true;
}

Related

How to ensure to run some code on same background thread?

I am using realm in my iOS Swift project. Search involve complex filters for a big data set. So I am fetching records on background thread.
But realm can be used only from same thread on which Realm was created.
I am saving a reference of results which I got after searching Realm on background thread. This object can only be access from same back thread
How can I ensure to dispatch code at different time to the same thread?
I tried below as suggested to solve the issue, but it didn't worked
let realmQueue = DispatchQueue(label: "realm")
var orginalThread:Thread?
override func viewDidLoad() {
super.viewDidLoad()
realmQueue.async {
self.orginalThread = Thread.current
}
let deadlineTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
self.realmQueue.async {
print("realm queue after some time")
if self.orginalThread == Thread.current {
print("same thread")
}else {
print("other thread")
}
}
}
}
Output is
realm queue after some time
other thread
Here's a small worker class that can works in a similar fashion to async dispatching on a serial queue, with the guarantee that the thread stays the same for all work items.
// Performs submitted work items on a dedicated thread
class Worker {
// the worker thread
private var thread: Thread?
// used to put the worker thread in the sleep mode, so in won't consume
// CPU while the queue is empty
private let semaphore = DispatchSemaphore(value: 0)
// using a lock to avoid race conditions if the worker and the enqueuer threads
// try to update the queue at the same time
private let lock = NSRecursiveLock()
// and finally, the glorious queue, where all submitted blocks end up, and from
// where the worker thread consumes them
private var queue = [() -> Void]()
// enqueues the given block; the worker thread will execute it as soon as possible
public func enqueue(_ block: #escaping () -> Void) {
// add the block to the queue, in a thread safe manner
locked { queue.append(block) }
// signal the semaphore, this will wake up the sleeping beauty
semaphore.signal()
// if this is the first time we enqueue a block, detach the thread
// this makes the class lazy - it doesn't dispatch a new thread until the first
// work item arrives
if thread == nil {
thread = Thread(block: work)
thread?.start()
}
}
// the method that gets passed to the thread
private func work() {
// just an infinite sequence of sleeps while the queue is empty
// and block executions if the queue has items
while true {
// let's sleep until we get signalled that items are available
semaphore.wait()
// extract the first block in a thread safe manner, execute it
// if we get here we know for sure that the queue has at least one element
// as the semaphore gets signalled only when an item arrives
let block = locked { queue.removeFirst() }
block()
}
}
// synchronously executes the given block in a thread-safe manner
// returns the same value as the block
private func locked<T>(do block: () -> T) -> T {
lock.lock(); defer { lock.unlock() }
return block()
}
}
Just instantiate it and let it do the job:
let worker = Worker()
worker.enqueue { print("On background thread, yay") }
You have to create your own thread with a run loop for that. Apple gives an example for a custom run loop in Objective C. You may create a thread class in Swift with that like:
class MyThread: Thread {
public var runloop: RunLoop?
public var done = false
override func main() {
runloop = RunLoop.current
done = false
repeat {
let result = CFRunLoopRunInMode(.defaultMode, 10, true)
if result == .stopped {
done = true
}
}
while !done
}
func stop() {
if let rl = runloop?.getCFRunLoop() {
CFRunLoopStop(rl)
runloop = nil
done = true
}
}
}
Now you can use it like this:
let thread = MyThread()
thread.start()
sleep(1)
thread.runloop?.perform {
print("task")
}
thread.runloop?.perform {
print("task 2")
}
thread.runloop?.perform {
print("task 3")
}
Note: The sleep is not very elegant but needed, since the thread needs some time for its startup. It should be better to check if the property runloop is set, and perform the block later if necessary. My code (esp. runloop) is probably not safe for race conditions, and it's only for demonstration. ;-)

Get notification from nested async call with dispatch_group_async

So I have situation when I've want do some async stuff, post status on Facebook and get notified when it's done posting. I was hoping that dispatch_group_async will do the job but now I hit the wall.
Here is how at this moment logic look like.
Func
func saveAndPost() {
//1. save
dispatch_group_async(group, queue) { () -> Void in
print("saving to DB")
//some func
print("saved to DB")
}
//2. post
dispatch_group_async(group, queue, { () -> Void in
print("publishing on FB")
//some func
//async request to ACAccount {
print("access")
//async performRequestWithHandler{
print("posted")
//anwser from request
}
}
}
print("published on FB")
})
dispatch_group_notify(group, queue, { () -> Void in
print("done")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//go to next view
})
})
}
Output
saving to DB
saved to DB
publishing on FB
published on FB
done
post
posted
My goal is to get notified when whole posting process is done. Maybe this is not possible with dispatch_group_async and I have to use KVO or even PromiseKit?
SOLUTION
David got me thinking and dispatch_group_enter & 'dispatch_group_leave' right what I needed. So at this moment my logic look like this.
func saveAndPost() {
//1. save
dispatch_group_async(group, queue) { () -> Void in
print("saving to DB")
//some func
print("saved to DB")
}
//2. post
dispatch_group_enter(group)
print("publishing on FB")
//some func
//async request to ACAccount {
print("access")
//async performRequestWithHandler{
print("posted")
//anwser from request
dispatch_group_leave(group)
}
}
}
print("published on FB")
dispatch_group_notify(group, queue, { () -> Void in
print("done")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//go to next view
})
})
}
As you can see part 2. is now modified.
My preferred method to tackle this when dealing with async calls:
var asyncGroup = dispatch_group_create()
dispatch_group_enter(asyncGroup)
asyncCall1.start() {
//end of callback
dispatch_group_leave(asyncGroup)
}
dispatch_group_enter(asyncGroup)
asyncCall2.start() {
asyncCall3.start() {
asyncCall4.start() {
dispatch_group_leave(asyncGroup)
}
}
}
dispatch_group_notify(asyncGroup, dispatch_get_main_queue(), {
println("done")
})
asyncCall1.start() { ... } and asyncCall2.start() { ... } are just sorta psuedocode to show you how it works.
Every dispatch_group_enter call needs to be balanced by a dispatch_group_leave call or you'll never get into the dispatch_group_notify block.

How to use callbacks to make Asynchronous calls in Swift so that main UI does not Hang?

I am trying to make Asynchronous call (to Parse) and during the call I DON'T want my Main UI to get hung.
Below is the function I am trying to call my ViewController. Below that function is the code I am using from my ViewController.
In the line sleep(4) line the main UI of the ViewController LoginVC does get stuck. Am I not using callbacks for the Asynchronous call correctly?
class Utils {
func logIntoWebService(uname: String, pwd: String, completion: ((result:Bool?) -> Void)!) {
PFUser.logInWithUsernameInBackground(uname, password:pwd) {
(user, error) -> Void in
if error == nil {
if user != nil {
// Yes, User Exists
//UI IS STUCK DURING THIS SLEEP(10)
sleep(10)
completion(result: true)
} else {
completion(result: false)
}
} else {
completion(result: false)
}
}
}
}
I am calling the above function from my ViewController. Below is the call from my ViewController
class LoginVC: UIViewController {
var mUtils: Utils = Utils()
mUtils.loggedInStatus({ (result) -> Void in
println("Response from logInToParse = " + String(stringInterpolationSegment: result))
})
}
You are getting confuse between background threads and completion handler.
logInWithUsernameInBackground is a async function that runs in the background however all the code that runs in the completion handler runs back in the main thread:
completion: ((result:Bool?) -> Void)!) {//The code here runs in the main thread}
So basically from the time that your application start communicate with Parse.com, until the result was back, that code was running asynchronous in a background thread, when it finish and your application received the response it runs in the completion block back in the main thread.
Now lets say you really want to run some code in the background in the completion block, than you would use something like:
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
println("Now this code will run in the background thread")
})
All the new quality of service classes are:
QOS_CLASS_USER_INTERACTIVE
QOS_CLASS_USER_INITIATED
QOS_CLASS_UTILITY
QOS_CLASS_BACKGROUND
Or in your case:
PFUser.logInWithUsernameInBackground(uname, password:pwd) {
(user, error) -> Void in
if error == nil {
if user != nil {
// Yes, User Exists
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
sleep(10)
})
completion(result: true)
} else {
completion(result: false)
}
} else {
completion(result: false)
}
}
For more information see Apple documentation

How to use background thread in swift?

How to use threading in swift?
dispatchOnMainThread:^{
NSLog(#"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];
Swift 3.0+
A lot has been modernized in Swift 3.0. Running something on a background queue looks like this:
DispatchQueue.global(qos: .userInitiated).async {
print("This is run on a background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
}
Swift 1.2 through 2.3
let qualityOfServiceClass = QOS_CLASS_USER_INITIATED
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
print("This is run on a background queue")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("This is run on the main queue, after the previous code in outer block")
})
})
Pre Swift 1.2 – Known issue
As of Swift 1.1 Apple didn't support the above syntax without some modifications. Passing QOS_CLASS_USER_INITIATED didn't actually work, instead use Int(QOS_CLASS_USER_INITIATED.value).
For more information see Apples documentation
Dan Beaulieu's answer in swift5 (also working since swift 3.0.1).
Swift 5.0.1
extension DispatchQueue {
static func background(delay: Double = 0.0, background: (()->Void)? = nil, completion: (() -> Void)? = nil) {
DispatchQueue.global(qos: .background).async {
background?()
if let completion = completion {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
completion()
})
}
}
}
}
Usage
DispatchQueue.background(delay: 3.0, background: {
// do something in background
}, completion: {
// when background job finishes, wait 3 seconds and do something in main thread
})
DispatchQueue.background(background: {
// do something in background
}, completion:{
// when background job finished, do something in main thread
})
DispatchQueue.background(delay: 3.0, completion:{
// do something in main thread after 3 seconds
})
The best practice is to define a reusable function that can be accessed multiple times.
REUSABLE FUNCTION:
e.g. somewhere like AppDelegate.swift as a Global Function.
func backgroundThread(_ delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
background?()
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) {
completion?()
}
}
}
Note: in Swift 2.0, replace QOS_CLASS_USER_INITIATED.value above with QOS_CLASS_USER_INITIATED.rawValue instead
USAGE:
A. To run a process in the background with a delay of 3 seconds:
backgroundThread(3.0, background: {
// Your background function here
})
B. To run a process in the background then run a completion in the foreground:
backgroundThread(background: {
// Your function here to run in the background
},
completion: {
// A function to run in the foreground when the background thread is complete
})
C. To delay by 3 seconds - note use of completion parameter without background parameter:
backgroundThread(3.0, completion: {
// Your delayed function here to be run in the foreground
})
In Swift 4.2 and Xcode 10.1
We have three types of Queues :
1. Main Queue:
Main queue is a serial queue which is created by the system and associated with the application main thread.
2. Global Queue :
Global queue is a concurrent queue which we can request with respect to the priority of the tasks.
3. Custom queues : can be created by the user. Custom concurrent queues always mapped into one of the global queues by specifying a Quality of Service property (QoS).
DispatchQueue.main//Main thread
DispatchQueue.global(qos: .userInitiated)// High Priority
DispatchQueue.global(qos: .userInteractive)//High Priority (Little Higher than userInitiated)
DispatchQueue.global(qos: .background)//Lowest Priority
DispatchQueue.global(qos: .default)//Normal Priority (after High but before Low)
DispatchQueue.global(qos: .utility)//Low Priority
DispatchQueue.global(qos: .unspecified)//Absence of Quality
These all Queues can be executed in two ways
1. Synchronous execution
2. Asynchronous execution
DispatchQueue.global(qos: .background).async {
// do your job here
DispatchQueue.main.async {
// update ui here
}
}
//Perform some task and update UI immediately.
DispatchQueue.global(qos: .userInitiated).async {
// Perform task
DispatchQueue.main.async {
// Update UI
self.tableView.reloadData()
}
}
//To call or execute function after some time
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
//Here call your function
}
//If you want to do changes in UI use this
DispatchQueue.main.async(execute: {
//Update UI
self.tableView.reloadData()
})
From AppCoda : https://www.appcoda.com/grand-central-dispatch/
//This will print synchronously means, it will print 1-9 & 100-109
func simpleQueues() {
let queue = DispatchQueue(label: "com.appcoda.myqueue")
queue.sync {
for i in 0..<10 {
print("🔴", i)
}
}
for i in 100..<110 {
print("Ⓜ️", i)
}
}
//This will print asynchronously
func simpleQueues() {
let queue = DispatchQueue(label: "com.appcoda.myqueue")
queue.async {
for i in 0..<10 {
print("🔴", i)
}
}
for i in 100..<110 {
print("Ⓜ️", i)
}
}
Swift 3 version
Swift 3 utilizes new DispatchQueue class to manage queues and threads. To run something on the background thread you would use:
let backgroundQueue = DispatchQueue(label: "com.app.queue", qos: .background)
backgroundQueue.async {
print("Run on background thread")
}
Or if you want something in two lines of code:
DispatchQueue.global(qos: .background).async {
print("Run on background thread")
DispatchQueue.main.async {
print("We finished that.")
// only back on the main thread, may you access UI:
label.text = "Done."
}
}
You can also get some in-depth info about GDC in Swift 3 in this tutorial.
From Jameson Quave's tutorial
Swift 2
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//All stuff here
})
Swift 4.x
Put this in some file:
func background(work: #escaping () -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
work()
}
}
func main(work: #escaping () -> ()) {
DispatchQueue.main.async {
work()
}
}
and then call it where you need:
background {
//background job
main {
//update UI (or what you need to do in main thread)
}
}
Swift 5
To make it easy, create a file "DispatchQueue+Extensions.swift" with this content :
import Foundation
typealias Dispatch = DispatchQueue
extension Dispatch {
static func background(_ task: #escaping () -> ()) {
Dispatch.global(qos: .background).async {
task()
}
}
static func main(_ task: #escaping () -> ()) {
Dispatch.main.async {
task()
}
}
}
Usage :
Dispatch.background {
// do stuff
Dispatch.main {
// update UI
}
}
You have to separate out the changes that you want to run in the background from the updates you want to run on the UI:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// do your task
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
}
Since the OP question has already been answered above I just want to add some speed considerations:
I don't recommend running tasks with the .background thread priority especially on the iPhone X where the task seems to be allocated on the low power cores.
Here is some real data from a computationally intensive function that reads from an XML file (with buffering) and performs data interpolation:
Device name / .background / .utility / .default / .userInitiated / .userInteractive
iPhone X: 18.7s / 6.3s / 1.8s / 1.8s / 1.8s
iPhone 7: 4.6s / 3.1s / 3.0s / 2.8s / 2.6s
iPhone 5s: 7.3s / 6.1s / 4.0s / 4.0s / 3.8s
Note that the data set is not the same for all devices. It's the biggest on the iPhone X and the smallest on the iPhone 5s.
Good answers though, anyway I want to share my Object Oriented solution Up to date for swift 5.
please check it out: AsyncTask
Conceptually inspired by android's AsyncTask, I've wrote my own class in Swift
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread.
Here are few usage examples
Example 1 -
AsyncTask(backgroundTask: {(p:String)->Void in//set BGParam to String and BGResult to Void
print(p);//print the value in background thread
}).execute("Hello async");//execute with value 'Hello async'
Example 2 -
let task2=AsyncTask(beforeTask: {
print("pre execution");//print 'pre execution' before backgroundTask
},backgroundTask:{(p:Int)->String in//set BGParam to Int & BGResult to String
if p>0{//check if execution value is bigger than zero
return "positive"//pass String "poitive" to afterTask
}
return "negative";//otherwise pass String "negative"
}, afterTask: {(p:String) in
print(p);//print background task result
});
task2.execute(1);//execute with value 1
It has 2 generic types:
BGParam - the type of the parameter sent to the task upon execution.
BGResult - the type of the result of the background computation.
When you create an AsyncTask you can those types to whatever you need to pass in and out of the background task, but if you don't need those types, you can mark it as unused with just setting it to: Void or with shorter syntax: ()
When an asynchronous task is executed, it goes through 3 steps:
beforeTask:()->Void invoked on the UI thread just before the task is executed.
backgroundTask: (param:BGParam)->BGResult invoked on the background thread immediately after
afterTask:(param:BGResult)->Void invoked on the UI thread with result from the background task
Multi purpose function for thread
public enum QueueType {
case Main
case Background
case LowPriority
case HighPriority
var queue: DispatchQueue {
switch self {
case .Main:
return DispatchQueue.main
case .Background:
return DispatchQueue(label: "com.app.queue",
qos: .background,
target: nil)
case .LowPriority:
return DispatchQueue.global(qos: .userInitiated)
case .HighPriority:
return DispatchQueue.global(qos: .userInitiated)
}
}
}
func performOn(_ queueType: QueueType, closure: #escaping () -> Void) {
queueType.queue.async(execute: closure)
}
Use it like :
performOn(.Background) {
//Code
}
I really like Dan Beaulieu's answer, but it doesn't work with Swift 2.2 and I think we can avoid those nasty forced unwraps!
func backgroundThread(delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
background?()
if let completion = completion{
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) {
completion()
}
}
}
}
Grand Central Dispatch is used to handle multitasking in our iOS apps.
You can use this code
// Using time interval
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
print("Hello World")
}
// Background thread
queue.sync {
for i in 0..<10 {
print("Hello", i)
}
}
// Main thread
for i in 20..<30 {
print("Hello", i)
}
More information use this link : https://www.programminghub.us/2018/07/integrate-dispatcher-in-swift.html
Is there a drawback (when needing to launch a foreground screen afterward) to the code below?
import Foundation
import UIKit
class TestTimeDelay {
static var connected:Bool = false
static var counter:Int = 0
static func showAfterDelayControl(uiViewController:UIViewController) {
NSLog("TestTimeDelay", "showAfterDelayControl")
}
static func tryReconnect() -> Bool {
counter += 1
NSLog("TestTimeDelay", "Counter:\(counter)")
return counter > 4
}
static func waitOnConnectWithDelay(milliseconds:Int, uiViewController: UIViewController) {
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(milliseconds), execute: {
waitOnConnect(uiViewController: uiViewController)
})
}
}
static func waitOnConnect(uiViewController:UIViewController) {
connected = tryReconnect()
if connected {
showAfterDelayControl(uiViewController: uiViewController)
}
else {
waitOnConnectWithDelay(milliseconds: 200, uiViewController:uiViewController)
}
}
}
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {
// Conversion into base64 string
self.uploadImageString = uploadPhotoDataJPEG.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.EncodingEndLineWithCarriageReturn)
})
in Swift 4.2 this works.
import Foundation
class myThread: Thread
{
override func main() {
while(true) {
print("Running in the Thread");
Thread.sleep(forTimeInterval: 4);
}
}
}
let t = myThread();
t.start();
while(true) {
print("Main Loop");
sleep(5);
}

dispatch_after equivalent in NSOperationQueue

I'm moving my code from regular GCD to NSOperationQueue because I need some of the functionality. A lot of my code relies on dispatch_after in order to work properly. Is there a way to do something similar with an NSOperation?
This is some of my code that needs to be converted to NSOperation. If you could provide an example of converting it using this code, that would be great.
dispatch_queue_t queue = dispatch_queue_create("com.cue.MainFade", NULL);
dispatch_time_t mainPopTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeRun * NSEC_PER_SEC));
dispatch_after(mainPopTime, queue, ^(void){
if(dFade !=nil){
double incriment = ([dFade volume] / [self fadeOut])/10; //incriment per .1 seconds.
[self doDelayFadeOut:incriment with:dFade on:dispatch_queue_create("com.cue.MainFade", 0)];
}
});
NSOperationQueue doesn't have any timing mechanism in it. If you need to set up a delay like this and then execute an operation, you'll want to schedule the NSOperation from the dispatch_after in order to handle both the delay and making the final code an NSOperation.
NSOperation is designed to handle more-or-less batch operations. The use case is slightly different from GCD, and in fact uses GCD on platforms with GCD.
If the problem you are trying to solve is to get a cancelable timer notification, I'd suggest using NSTimer and invalidating it if you need to cancel it. Then, in response to the timer, you can execute your code, or use a dispatch queue or NSOperationQueue.
You can keep using dispatch_after() with a global queue, then schedule the operation on your operation queue. Blocks passed to dispatch_after() don't execute after the specified time, they are simply scheduled after that time.
Something like:
dispatch_after
(
mainPopTime,
dispatch_get_main_queue(),
^ {
[myOperationQueue addOperation:theOperationObject];
}
);
Seven years late, but iOS 7 introduced this functionality to OperationQueue.
https://developer.apple.com/documentation/foundation/operationqueue/3329365-schedule
You could make an NSOperation that performs a sleep: MYDelayOperation. Then add it as a dependency for your real work operation.
#interface MYDelayOperation : NSOperation
...
- (void)main
{
[NSThread sleepForTimeInterval:delay]; // delay is passed in constructor
}
Usage:
NSOperation *theOperationObject = ...
MYDelayOperation *delayOp = [[MYDelayOperation alloc] initWithDelay:5];
[theOperationObject addDependency:delayOp];
[myOperationQueue addOperations:#[ delayOp, theOperationObject] waitUntilFinished:NO];
[operationQueue performSelector:#selector(addOperation:)
withObject:operation
afterDelay:delay];
Swift 4:
DispatchQueue.global().asyncAfter(deadline: .now() + 10 { [weak self] in
guard let `self` = self else {return}
self. myOperationQueue.addOperation {
//...code...
}
}
I used following code to get delayed call of operation:
class DelayedBlockOperation: Operation {
private let deadline: DispatchTime
private let block: (() -> Void)?
private let queue: DispatchQueue
override var isAsynchronous: Bool { true }
override var isExecuting: Bool {
get { _executing }
set {
willChangeValue(forKey: "isExecuting")
_executing = newValue
didChangeValue(forKey: "isExecuting")
}
}
private var _executing: Bool = false
override var isFinished: Bool {
get { _finished }
set {
willChangeValue(forKey: "isFinished")
_finished = newValue
didChangeValue(forKey: "isFinished")
}
}
private var _finished: Bool = false
init(deadline: DispatchTime,
queue: DispatchQueue = .global(),
_ block: #escaping () -> Void = { }) {
self.deadline = deadline
self.queue = queue
self.block = block
}
override func start() {
queue.asyncAfter(deadline: deadline) {
guard !self.isCancelled else {
self.isFinished = true
return
}
guard let block = self.block else {
self.isFinished = true
return
}
self.isExecuting = true
block()
self.isFinished = true
}
}
}
I was inspired by the gist

Resources