Repeating tasks, e.g. fetching APIs, are tasks/block codes need to be excuted every interval time. I sometimes have to deal with them. And they sometimes become massive with a lot of timer and release timer things. So I decide to write a simple short-polling center in Swift. Short-polling repo
What I need a polling center do
Excute a task every interval
Excute tasks in a single background queue
Easy to enable/disable a polling
Seperate each polling
What I will implement
Protocol for polling item. So I can create my own polling item easily.
Have uid so I can manage them
Can config time interval
Have a handler for excuting
Polling center. For managing polling item.
Singleton object
Have a queue for every tasks can excute in background queue
Have a timer for each Pollingable item
Enable/disable Pollingable item in a queue also
1. Protocol for Pollingable item
protocolPollingable {// default uid for pollingfuncuid() ->Stringstaticfuncuid() ->String// interval in secondfuncinterval() ->Int// invoke handler every intervalfunceventHandler() -> (() ->Void)}extensionPollingablewhere Self:NSObject {funcuid() ->String {returnString(describing: Self.self) }staticfuncuid() ->String {returnString(describing: Self.self) }}