Selene是一个iOS库,用于在后台调度任务的执行。
使用:
1)添加fetch后台模式到你app的Info.plist文件。
2)创建一个任务
任务必须符合SLNTaskProtocol。例如:
@interface SampleTask: NSObject<SLNTaskProtocol>@end@implementation SampleTask+ (NSString *)identifier { return NSStringFromClass(self);}+ (NSOperation *)operationWithCompletion:(SLNTaskCompletion_t)completion { NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ // Do some work .... completion(UIBackgroundFetchResultNoData); }]; return operation;}+ (CGFloat)averageResponseTime { return 5.0;}+ (SLNTaskPriority)priority { return SLNTaskPriorityLow;}@end3)添加任务类调度
NSArray *tasks = @[[SomeTask class]];// Run the scheduler every 5 minutes[SLNScheduler setMinimumBackgroundFetchInterval:60 * 5];// Add the tasks[SLNScheduler scheduleTasks:tasks];在应用程序委托:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [SLNScheduler startWithCompletion:completionHandler];}
评论