Selene 是一个 iOS 库,用于在后台调度任务的执行。
使用:
1) 添加fetch 后台模式到你 app 的 Info.plist 文件。
fetch
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; } @end
3) 添加任务类调度
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]; }