mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-05 10:28:54 +00:00
* Guard long-running DB chains against RUNNINGBOARD 0xdead10cc on iOS iOS can terminate the app if it's suspended while a SQLite statement still holds the shared App Group database lock. AppDelegate already held a single background-task assertion across the background transition, but it was a blind, fixed-duration timer: it never tracked whether a DB operation was actually still running, and a chain that started after the timer had already begun (or ran long enough to outlast it) got no protection. Add DatabaseActivityAssertion (a Swift port of Quinn "The Eskimo!"'s QRunInBackgroundAssertion) and DatabaseLockProtectionManager, which lets any caller - the app lifecycle transition, or a specific long-running chain - acquire its own independent background-task job and release it exactly when its own work finishes. Wire this into the websocket reconnect sync (doReconnect) and the deferred per-team entry sync (restDeferredAppEntryActions), the two longest sequential local-DB chains in the app, via a new pair of RNUtils methods (iOS only; Android is a no-op, since 0xdead10cc has no Android equivalent). * fix tests
42 lines
1.7 KiB
Swift
42 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
/// Owns one `DatabaseActivityAssertion` per caller, keyed by a generated token, so each
|
|
/// job's assertion is released independently. Used by both `AppDelegate`'s lifecycle
|
|
/// transitions and JS (via RNUtils) around specific long-running DB chains.
|
|
public class DatabaseLockProtectionManager: NSObject {
|
|
@objc public static let shared = DatabaseLockProtectionManager()
|
|
|
|
private var assertions: [String: DatabaseActivityAssertion] = [:]
|
|
|
|
private override init() {}
|
|
|
|
/// Returns `nil` if the OS declined to grant a background task. Must be called on the main thread.
|
|
@objc public func begin(_ serverUrl: String, task: String) -> String? {
|
|
dispatchPrecondition(condition: .onQueue(.main))
|
|
|
|
let encodedServerUrl = Data(serverUrl.utf8).base64EncodedString()
|
|
.replacingOccurrences(of: "+", with: "-")
|
|
.replacingOccurrences(of: "/", with: "_")
|
|
.replacingOccurrences(of: "=", with: "")
|
|
let timestampMs = Int(Date().timeIntervalSince1970 * 1000)
|
|
let token = "\(encodedServerUrl)-\(task)-\(timestampMs)"
|
|
|
|
let assertion = DatabaseActivityAssertion(name: task)
|
|
guard assertion.isGranted else { return nil }
|
|
|
|
assertion.systemDidReleaseAssertion = { [weak self] in
|
|
self?.assertions.removeValue(forKey: token)
|
|
}
|
|
assertions[token] = assertion
|
|
return token
|
|
}
|
|
|
|
/// Safe to call with an unknown/already-ended token (no-op). Must be called on the main thread.
|
|
@objc public func end(_ token: String) {
|
|
dispatchPrecondition(condition: .onQueue(.main))
|
|
|
|
guard let assertion = assertions.removeValue(forKey: token) else { return }
|
|
assertion.release()
|
|
}
|
|
}
|