SHARE
TWEET

AsyncTask

NikitaKurtin Feb 4th, 2016 (edited) 1,965 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Foundation
  2. /**
  3.  *AsyncTask v1.2 Updated: 2019-03-30
  4.  *Defines a task which executed asynchronously in background thread.
  5.  *Every AsyncTask instance has 3 life cycle events:
  6.  * 1. beforeTask execution (Optional) - executed on UI Main thread
  7.  * 2. bagkroundTask execution - executed in background thread
  8.  * 3. afterTask execution (Optional) - executed on UI Main thread
  9.  *
  10.  *When caller instantiates AsyncTask he\she can decide what data type to pass in and out, using
  11.  * predefined generic types <BGParam,BGResult> where
  12.  * BGParam - passed in to 'backgroundTask' from calling 'execute' method
  13.  * BGResult - passed out from 'backgroundTask' to 'afterTask' method
  14.  *
  15.  *Usage examples:
  16.  *
  17.  * //Example 1
  18.  *   AsyncTask(backgroundTask: {(p:String)->() in
  19.  *     print(p);
  20.  *   }).execute("Hello async");
  21.  *
  22.  * //Example 2
  23.  *   let task=AsyncTask(beforeTask: {
  24.  *       print("pre execution");
  25.  *   },backgroundTask: {(p:Int)->String in
  26.  *      if p>=0{return "Positive";}
  27.  *      else {return "Negative";}
  28.  *   }, afterTask: {(p:String)in
  29.  *      print("\(p)");
  30.  *   });
  31.  *   task.execute(5);
  32.  *
  33.  */
  34. public class AsyncTask <BGParam,BGResult>{
  35.     private var pre:(()->())?;//Optional closure -> called before the backgroundTask
  36.     private var bgTask:(_ param:BGParam)->BGResult;//background task
  37.     private var post:((_ param:BGResult)->())?;//Optional closure -> called after the backgroundTask
  38.     /**
  39.      *@param beforeTask Optional closure -> which called just before the background task
  40.      *@param backgroundTask closure -> the background task functionality with generic param & return
  41.      *@param afterTask Optional -> which called just after the background task
  42.      */
  43.     public init(beforeTask: (()->())?=nil, backgroundTask: @escaping (_ param:BGParam)->BGResult, afterTask:((_ param:BGResult)->())?=nil){
  44.         self.pre=beforeTask;
  45.         self.bgTask=backgroundTask;
  46.         self.post=afterTask;
  47.     }
  48.     /**
  49.      *Execution method for current backgroundTask with given parameter value in background thread.
  50.      *@param BGParam passed as a parameter to backgroundTask
  51.      */
  52.     public func execute(_ param:BGParam){
  53.         pre?()//if beforeTask exists - invoke it before bgTask
  54.         DispatchQueue.global(qos: .background).async {
  55.             let bgResult=self.bgTask(param);//execute backgroundTask in background thread
  56.             if(self.post != nil){//if afterTask exists - invoke it in UI thread after bgTask
  57.                 DispatchQueue.main.async {self.post!(bgResult)}
  58.             }
  59.         }
  60.     }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top