2017-12-15 17:55:10 +01:00
|
|
|
//
|
2018-02-26 19:49:30 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-12-15 17:55:10 +01:00
|
|
|
//
|
|
|
|
|
2018-02-26 19:49:30 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-12-15 19:03:03 +01:00
|
|
|
typedef NS_ENUM(NSUInteger, BackgroundTaskState) {
|
|
|
|
BackgroundTaskState_Success,
|
|
|
|
BackgroundTaskState_CouldNotStart,
|
|
|
|
BackgroundTaskState_Expired,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (^BackgroundTaskCompletionBlock)(BackgroundTaskState backgroundTaskState);
|
|
|
|
|
2018-02-26 19:49:30 +01:00
|
|
|
// This class can be safely accessed and used from any thread.
|
|
|
|
@interface OWSBackgroundTaskManager : NSObject
|
|
|
|
|
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
|
|
|
|
|
|
|
+ (instancetype)sharedManager;
|
|
|
|
|
2018-02-27 17:06:05 +01:00
|
|
|
- (void)observeNotifications;
|
|
|
|
|
2018-02-26 19:49:30 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-12-15 19:52:39 +01:00
|
|
|
// This class makes it easier and safer to use background tasks.
|
|
|
|
//
|
|
|
|
// * Uses RAII (Resource Acquisition Is Initialization) pattern.
|
|
|
|
// * Ensures completion block is called exactly once and on main thread,
|
|
|
|
// to facilitate handling "background task timed out" case, for example.
|
|
|
|
// * Ensures we properly handle the "background task could not be created"
|
|
|
|
// case.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// * Use factory method to start a background task.
|
2017-12-15 19:53:56 +01:00
|
|
|
// * Retain a strong reference to the OWSBackgroundTask during the "work".
|
|
|
|
// * Clear all references to the OWSBackgroundTask when the work is done,
|
|
|
|
// if possible.
|
2017-12-15 17:55:10 +01:00
|
|
|
@interface OWSBackgroundTask : NSObject
|
|
|
|
|
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
2017-12-15 19:03:03 +01:00
|
|
|
|
|
|
|
+ (OWSBackgroundTask *)backgroundTaskWithLabelStr:(const char *)labelStr;
|
|
|
|
|
|
|
|
// completionBlock will be called exactly once on the main thread.
|
|
|
|
+ (OWSBackgroundTask *)backgroundTaskWithLabelStr:(const char *)labelStr
|
|
|
|
completionBlock:(BackgroundTaskCompletionBlock)completionBlock;
|
|
|
|
|
|
|
|
+ (OWSBackgroundTask *)backgroundTaskWithLabel:(NSString *)label;
|
|
|
|
|
|
|
|
// completionBlock will be called exactly once on the main thread.
|
|
|
|
+ (OWSBackgroundTask *)backgroundTaskWithLabel:(NSString *)label
|
|
|
|
completionBlock:(BackgroundTaskCompletionBlock)completionBlock;
|
2017-12-15 17:55:10 +01:00
|
|
|
|
|
|
|
@end
|
2018-02-26 19:49:30 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|