@ -31,7 +31,8 @@ public struct Job: Codable, Equatable, Identifiable, FetchableRecord, MutablePer
case failureCount
case variant
case behaviour
case shouldBlockFirstRunEachSession
case shouldBlock
case shouldSkipLaunchBecomeActive
case nextRunTimestamp
case threadId
case interactionId
@ -136,12 +137,16 @@ public struct Job: Codable, Equatable, Identifiable, FetchableRecord, MutablePer
// / H o w t h e j o b s h o u l d b e h a v e
public let behaviour : Behaviour
// / W h e n t h e a p p s t a r t s o r r e t u r n s f r o m t h e b a c k g r o u n d t h i s f l a g c o n t r o l s w h e t h e r t h e j o b s h o u l d p r e v e n t o t h e r
// / j o b s f r o m s t a r t i n g u n t i l a f t e r i t c o m p l e t e s
// / W h e n t h e a p p s t a r t s t h i s f l a g c o n t r o l s w h e t h e r t h e j o b s h o u l d p r e v e n t o t h e r j o b s f r o m s t a r t i n g u n t i l a f t e r i t c o m p l e t e s
// /
// / * * N o t e : * * ` O n L a u n c h ` b l o c k i n g j o b s w i l l b e s t a r t e d o n l a u n c h a n d a l l o t h e r s w i l l b e t r i g g e r e d w h e n b e c o m i n g
// / a c t i v e b u t t h e " b l o c k i n g " b e h a v i o u r w i l l o n l y o c c u r i f t h e r e a r e n o o t h e r j o b s a l r e a d y r u n n i n g
public let shouldBlockFirstRunEachSession : Bool
// / * * N o t e : * * T h i s f l a g i s o n l y s u p p o r t e d f o r j o b s w i t h a n ` O n L a u n c h ` b e h a v i o u r b e c a u s e t h e r e i s n o w a y t o g u a r a n t e e
// / j o b s w i t h a n y o t h e r b e h a v i o u r s w i l l b e a d d e d t o t h e J o b R u n n e r b e f o r e a l l t h e ` O n L a u n c h ` b l o c k i n g j o b s a r e c o m p l e t e d
// / r e s u l t i n g i n t h e J o b R u n n e r n o l o n g e r b l o c k i n g
public let shouldBlock : Bool
// / W h e n t h e a p p s t a r t s i t a l s o t r i g g e r s a n y ` O n A c t i v e ` j o b s , t h i s f l a g c o n t r o l s w h e t h e r t h e j o b s h o u l d s k i p t h i s i n i t i a l ` O n A c t i v e `
// / t r i g g e r ( g e n e r a l l y u s e d f o r t h e s a m e j o b r e g i s t e r e d w i t h b o t h ` O n L a u n c h ` a n d ` O n A c t i v e ` b e h a v i o u r s )
public let shouldSkipLaunchBecomeActive : Bool
// / S e c o n d s s i n c e e p o c h t o i n d i c a t e t h e n e x t d a t e t i m e t h a t t h i s j o b s h o u l d r u n
public let nextRunTimestamp : TimeInterval
@ -184,17 +189,25 @@ public struct Job: Codable, Equatable, Identifiable, FetchableRecord, MutablePer
failureCount : UInt ,
variant : Variant ,
behaviour : Behaviour ,
shouldBlockFirstRunEachSession : Bool ,
shouldBlock : Bool ,
shouldSkipLaunchBecomeActive : Bool ,
nextRunTimestamp : TimeInterval ,
threadId : String ? ,
interactionId : Int64 ? ,
details : Data ?
) {
Job . ensureValidBehaviour (
behaviour : behaviour ,
shouldBlock : shouldBlock ,
shouldSkipLaunchBecomeActive : shouldSkipLaunchBecomeActive
)
self . id = id
self . failureCount = failureCount
self . variant = variant
self . behaviour = behaviour
self . shouldBlockFirstRunEachSession = shouldBlockFirstRunEachSession
self . shouldBlock = shouldBlock
self . shouldSkipLaunchBecomeActive = shouldSkipLaunchBecomeActive
self . nextRunTimestamp = nextRunTimestamp
self . threadId = threadId
self . interactionId = interactionId
@ -205,15 +218,23 @@ public struct Job: Codable, Equatable, Identifiable, FetchableRecord, MutablePer
failureCount : UInt = 0 ,
variant : Variant ,
behaviour : Behaviour = . runOnce ,
shouldBlockFirstRunEachSession : Bool = false ,
shouldBlock : Bool = false ,
shouldSkipLaunchBecomeActive : Bool = false ,
nextRunTimestamp : TimeInterval = 0 ,
threadId : String ? = nil ,
interactionId : Int64 ? = nil
) {
Job . ensureValidBehaviour (
behaviour : behaviour ,
shouldBlock : shouldBlock ,
shouldSkipLaunchBecomeActive : shouldSkipLaunchBecomeActive
)
self . failureCount = failureCount
self . variant = variant
self . behaviour = behaviour
self . shouldBlockFirstRunEachSession = shouldBlockFirstRunEachSession
self . shouldBlock = shouldBlock
self . shouldSkipLaunchBecomeActive = shouldSkipLaunchBecomeActive
self . nextRunTimestamp = nextRunTimestamp
self . threadId = threadId
self . interactionId = interactionId
@ -224,13 +245,19 @@ public struct Job: Codable, Equatable, Identifiable, FetchableRecord, MutablePer
failureCount : UInt = 0 ,
variant : Variant ,
behaviour : Behaviour = . runOnce ,
shouldBlockFirstRunEachSession : Bool = false ,
shouldBlock : Bool = false ,
shouldSkipLaunchBecomeActive : Bool = false ,
nextRunTimestamp : TimeInterval = 0 ,
threadId : String ? = nil ,
interactionId : Int64 ? = nil ,
details : T ?
) {
precondition ( T . self != Job . self , " [Job] Fatal error trying to create a Job with a Job as it's details " )
Job . ensureValidBehaviour (
behaviour : behaviour ,
shouldBlock : shouldBlock ,
shouldSkipLaunchBecomeActive : shouldSkipLaunchBecomeActive
)
guard
let details : T = details ,
@ -240,13 +267,31 @@ public struct Job: Codable, Equatable, Identifiable, FetchableRecord, MutablePer
self . failureCount = failureCount
self . variant = variant
self . behaviour = behaviour
self . shouldBlockFirstRunEachSession = shouldBlockFirstRunEachSession
self . shouldBlock = shouldBlock
self . shouldSkipLaunchBecomeActive = shouldSkipLaunchBecomeActive
self . nextRunTimestamp = nextRunTimestamp
self . threadId = threadId
self . interactionId = interactionId
self . details = detailsData
}
fileprivate static func ensureValidBehaviour (
behaviour : Behaviour ,
shouldBlock : Bool ,
shouldSkipLaunchBecomeActive : Bool
) {
// B l o c k i n g j o b s c a n o n l y r u n o n l a u n c h a s w e c a n ' t g u a r a n t e e t h a t a n y o t h e r b e h a v i o u r s w i l l g e t a d d e d
// t o t h e J o b R u n n e r b e f o r e a n y p r i o r b l o c k i n g j o b s h a v e c o m p l e t e d ( r e s u l t i n g i n t h e m b e i n g n o n - b l o c k i n g )
precondition (
! shouldBlock || behaviour = = . recurringOnLaunch || behaviour = = . runOnceNextLaunch ,
" [Job] Fatal error trying to create a blocking job which doesn't run on launch "
)
precondition (
! shouldSkipLaunchBecomeActive || behaviour = = . recurringOnActive ,
" [Job] Fatal error trying to create a job which skips on 'OnActive' triggered during launch with doesn't run on active "
)
}
// MARK: - C u s t o m D a t a b a s e I n t e r a c t i o n
public mutating func didInsert ( with rowID : Int64 , for column : String ? ) {
@ -306,7 +351,8 @@ public extension Job {
failureCount : failureCount ,
variant : self . variant ,
behaviour : self . behaviour ,
shouldBlockFirstRunEachSession : self . shouldBlockFirstRunEachSession ,
shouldBlock : self . shouldBlock ,
shouldSkipLaunchBecomeActive : self . shouldSkipLaunchBecomeActive ,
nextRunTimestamp : nextRunTimestamp ,
threadId : self . threadId ,
interactionId : self . interactionId ,
@ -322,7 +368,8 @@ public extension Job {
failureCount : self . failureCount ,
variant : self . variant ,
behaviour : self . behaviour ,
shouldBlockFirstRunEachSession : self . shouldBlockFirstRunEachSession ,
shouldBlock : self . shouldBlock ,
shouldSkipLaunchBecomeActive : self . shouldSkipLaunchBecomeActive ,
nextRunTimestamp : self . nextRunTimestamp ,
threadId : self . threadId ,
interactionId : self . interactionId ,