I want to use a method that wait for getting some thing and then do some thing else - I have a progressive in my app and want to use a function that waiting for progressive to be 100 percent and then that function execute - in generally I want to know how can I use a method that execute when another things happens - I used group method many times but this is more different(its some thing like listener)
let myGroup = DispatchGroup()
myGroup.enter()
//// Do your task
//// When you task complete
myGroup.leave()
myGroup.notify(queue: DispatchQueue.main) {
////// do your remaining work
}
You can use notification
, delegate
or blocks
to do that. Check this answer for delegates. Here is an example for notifications. And this answer contains how to use completion handler blocks They are explained in details here.
I am updating this with delegate example :
//Do this in the class (assuming it as EventViewController) where event occurs
protocol delegateForEvent {
func didEventCompleted()
}
class EventViewController: UIViewController {
var delegate:delegateForEvent!
When the event completed, call this
self.delegate.didEventCompleted() // call this at the time of color change
And in ViewController or whichever class you want to receive the event (assuming it as ViewController.swift),
In ViewController.swift
class ViewController: UIViewController, delegateForEvent { // Set delegateForEvent in interface
set the delegate where you create your eventViewController instance in viewController.swift
myEventViewController.delegate = self; // set it where you created your object
and add the delegate function in viewController.swift
didEventCompleted{
//here you can do whatever you want to after the event completion
}