代碼請移步個人githubgit
iOS開發時, 咱們一般會在AppDelegate
的delegate method 中實現若干啓動須要的邏輯.如:github
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // do your massive stuff..... .... .... return YES; }
對於中大型的App來講,隨着業務愈來愈大, AppDelegate
中的代碼會成爲災難,難於管理.如何優雅的對其進行管理呢. 因此RWTaskKit
爲此而生, 今後告別AppDelegate
.objective-c
下面是一個在任意.m
中定義的代碼:app
@task(Task1, RWTaskPriorityCritical, RWTaskEventWillFinishLaunching) +(void) run { NSLog(@"This is No.1 task we want to perform!"); } @end
方法run
將在application:willFinishLaunchingWithOptions:
調用以後觸發,同時,咱們不用關心AppDelegate
, 只須要import RWTask.h
.ide
上述例子中能夠我能夠看到, 一個Task,由三部分組成:任務標識符
任務體
結束標識符
post
@
開頭task
schedule
timer
任務體ui
包括1到2個方法
@end
RWTaskKit
目前支持三種任務:event task
schedule task
notification task
this
Event task is designed to handle common methods calling in UIApplicationDelegate. code
Parameters in the brackets are :Task Name
, Priority
, Event Name
orm
Task Nanme
would be any unique string.Priority
would be enumeration value witch can be found in RWTaskObject.h
, task (for a same event) with higher priority will be exexuted earlier.
Event Name
would be enumeration value as following:
typedef NS_ENUM(NSUInteger, RWTaskEvent) { RWTaskEventWillFinishLaunching = 0x1, //tasks will be performed only once RWTaskEventDidFinishLaunching = 0x1 << 1, //tasks will be performed only once RWTaskEventWillEnterForeground = 0x1 << 2, RWTaskEventDidEnterBackground = 0x1 << 3, RWTaskEventDidBecomeActive = 0x1 << 4, RWTaskEventDidReceiveMemoryWarning = 0x1 << 5, RWTaskEventWillTerminate = 0x1 << 6, RWTaskEventWillResignActive = 0x1 << 7, RWTaskEventSignificantTimeChange = 0x1 << 8, RWTaskEventOpenURL = 0x1 << 9, RWTaskEventWillChangeStatusBarOrientation = 0x1 << 10, RWTaskEventDidChangeStatusBarOrientation = 0x1 << 11, RWTaskEventBackgroundRefreshStatusDidChange = 0x1 << 12, RWTaskEventUserDidTakeScreenshot = 0x1 << 13, RWTaskEventReserved1 = 0x1 << 14, RWTaskEventReserved2 = 0x1 << 15, RWTaskEventIdle = 0x1 << 16, // not implemented RWTaskEventNone = 0, };
Above event enumerations are mapped to coresponding delegate methods. see UIApplication.h
in iOS SDK.
class method run
is a must-have for any task. Besides event task
has an optional method +(NSArray<Class>*)dependency
which denotes the dependency of the task. See the following example:
@task(Task2, RWTaskPriorityCritical, RWTaskEventWillFinishLaunching) +(void) run { NSLog(@"This is No.2 task we want to perform!"); } +(NSArray<Class>*)dependency{ // this task will be executed after Task1 and Task3 finish return @[task_name(Task1),task_name(Task3)]; } @end
Here task_name
is a macro for convenience. Tasks specified in method dependency
have nothing to do with the order inside the array.
Schedule task is designed for the task should be executed right after a scheduled time or repeated with specified times.
examples:
@schedule(ScheduleTask1, 1448873913, 5, 1) + (void) run{ NSLog(@"hello"); } @end @timer(ScheduleTask2, 10, 2) + (void) run{ NSLog(@"hello"); } @end
ScheduleTask1
will be executed right after 1448873913
(timestamp, like [[NSDate date] timeIntervalSince1970]
), it will be executed 5
times for every 1
second.
ScheduleTask2
will be executed every 2
seconds, and it will repeat 10
times.
Schedule task has a built-in clock, the time interval is 1 second. Thus the timeinterval
in paramters should be an integral multiple of built-in time interval. Besides the maximum timeinterval
is 120 * built-in time interval.
Note: developers can customize the built-in time interval by trigger a repeatedly notification implused by your own
NSTimer
.First, use
NO_RW_CLOCK
macro only once in any.m
file. Second, post a notification with nameRWAppClockTicktockNotificationKey
in your customized timer.
Notification task is designed for task should be triggered by a notification. Example:
@notification(NotiTask,@"NotificationKey") + (void) runWithNotification:(NSNotification*)noti{ NSLog(@"hello"); } @end
Following interfaces can be used in runtime scenario to inject event
tasks and schedule
tasks.
- (void)injectEventTaskWithName:(NSString *)taskName andBlock:(RWTaskBlock)block priority:(RWTaskPriority)p triggerEvent:(RWTaskEvent)e dependency:(NSArray<Class> *)dependency autoRemove:(BOOL)autoRemove; - (void)scheduleTaskWithName:(NSString *)taskName andBlock:(RWTaskBlock)block desiredDate:(NSDate *)date repeatCount:(NSInteger)count timeInterval:(NSUInteger)timeInterval; - (void)destroyTaskByName:(NSString *)taskName; - (void)pauseTaskByName:(NSString *)taskName; - (void)resumeTaskByName:(NSString *)taskName;