PSOperations
PSOperations copied to clipboard
"evaluateConditions() was called out-of-order" assert in Operation.swift is sometimes failing
I have a rather big code base that was using the the code based on the the WWDC presentation code. I recently discovered PSOperations and updated our code base using that. Now for some reason the assert in Operation class evaluateConditions is failing when my operation inherited form GroupOperation gets cancelled.
I'm puzzled since the failure occurs rarely. The code base uses Operations and especially GroupOperatins heavily in down loading and parsing data from REST sources.
Why am I getting the assertion failure?
I'm using code from master (latest commit is 7947757c25181013df61a00bb538bfd0982e383f)
We have made many changes around race conditions, are you seeing this problem with the latest version?
I have encountered this issue as well and i have found a 100% reproducible case.
- Create a condition that is async (do a dispatch after 1 second and call completion with any result)
- Add the operation with that condition to queue and cancel immediately
- The operation will crash in debug build due to this assert, since the
var isReady: Bool
is returningtrue
when the operation is cancelled, butstate
at this point is still.evaluatingConditions
What i did to fix this:
// Here is where we extend our definition of "readiness".
override open var isReady: Bool {
stateAccess.lock()
defer { stateAccess.unlock() }
guard super.isReady else { return false }
guard !isCancelled else {
// when operation gets cancelled during evaluate conditions an assert is raised, so prevent transition to `isReady` until conditions are finished evaluating since there's no cancellation of conditions
return state != .evaluatingConditions
}
switch state {
case .initialized, .evaluatingConditions, .pending:
return false
case .ready, .executing, .finishing, .finished:
return true
}
}