mirror of https://github.com/oxen-io/session-ios
Create JobQueue
parent
28172b4ed2
commit
d80804ca5a
@ -1,2 +1,9 @@
|
||||
|
||||
public protocol Job { }
|
||||
public protocol Job : class {
|
||||
var delegate: JobDelegate? { get set }
|
||||
var failureCount: UInt { get set }
|
||||
|
||||
static var maxFailureCount: UInt { get }
|
||||
|
||||
func execute()
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
|
||||
public protocol JobDelegate {
|
||||
|
||||
func handleJobSucceeded(_ job: Job)
|
||||
func handleJobFailed(_ job: Job, with error: Error)
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
|
||||
public final class JobQueue : JobDelegate {
|
||||
|
||||
public static let shared = JobQueue()
|
||||
|
||||
public func add(_ job: Job, using transaction: Any) {
|
||||
Configuration.shared.storage.persist(job, using: transaction)
|
||||
job.delegate = self
|
||||
job.execute()
|
||||
}
|
||||
|
||||
public func handleJobSucceeded(_ job: Job) {
|
||||
// Mark the job as succeeded
|
||||
}
|
||||
|
||||
public func handleJobFailed(_ job: Job, with error: Error) {
|
||||
// Persist the job
|
||||
// Retry it if the max failure count hasn't been reached
|
||||
// Propagate the error otherwise
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSTimer (Session)
|
||||
|
||||
// This method avoids the classic NSTimer retain cycle bug by using a weak reference to the target
|
||||
+ (NSTimer *)weakScheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
|
||||
target:(id)target
|
||||
selector:(SEL)selector
|
||||
userInfo:(nullable id)userInfo
|
||||
repeats:(BOOL)repeats;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,64 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "NSTimer+Proxying.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSTimerProxy : NSObject
|
||||
|
||||
@property (nonatomic, weak) id target;
|
||||
@property (nonatomic) SEL selector;
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSTimerProxy
|
||||
|
||||
- (void)timerFired:(NSDictionary *)userInfo
|
||||
{
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
|
||||
[self.target performSelector:self.selector withObject:userInfo];
|
||||
#pragma clang diagnostic pop
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
static void *kNSTimer_SN_Proxy = &kNSTimer_SN_Proxy;
|
||||
|
||||
@implementation NSTimer (Session)
|
||||
|
||||
- (NSTimerProxy *)sn_proxy
|
||||
{
|
||||
return objc_getAssociatedObject(self, kNSTimer_SN_Proxy);
|
||||
}
|
||||
|
||||
- (void)sn_setProxy:(NSTimerProxy *)proxy
|
||||
{
|
||||
#if DEBUG
|
||||
assert(proxy != nil);
|
||||
#endif
|
||||
|
||||
objc_setAssociatedObject(self, kNSTimer_SN_Proxy, proxy, OBJC_ASSOCIATION_RETAIN);
|
||||
}
|
||||
|
||||
+ (NSTimer *)weakScheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
|
||||
target:(id)target
|
||||
selector:(SEL)selector
|
||||
userInfo:(nullable id)userInfo
|
||||
repeats:(BOOL)repeats
|
||||
{
|
||||
NSTimerProxy *proxy = [NSTimerProxy new];
|
||||
proxy.target = target;
|
||||
proxy.selector = selector;
|
||||
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
|
||||
target:proxy
|
||||
selector:@selector(timerFired:)
|
||||
userInfo:userInfo
|
||||
repeats:repeats];
|
||||
[timer sn_setProxy:proxy];
|
||||
return timer;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue