Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS-8546: Move CHHapticEngine init on background thread #392

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions TangemSdk/TangemSdk/UI/Main/HapticsEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,32 @@ class HapticsEngine {
return
}

do {
engine = try CHHapticEngine()
engine!.playsHapticsOnly = true
engine!.stoppedHandler = {[weak self] reason in
Log.debug("CHHapticEngine stop handler: The engine stopped for reason: \(reason.rawValue)")
self?.engineNeedsStart = true
}
engine!.resetHandler = {[weak self] in
Log.debug("Reset Handler: Restarting the engine.")
do {
// Try restarting the engine.
try self?.engine?.start()

// Indicate that the next time the app requires a haptic, the app doesn't need to call engine.start().
self?.engineNeedsStart = false

} catch {
Log.error("Failed to start the engine with error: \(error)")
/// We need to instantiate `CHHapticEngine` on a background thread
/// because on the Main thread I/O operations can cause UI unresponsiveness
DispatchQueue.global(qos: .userInitiated).async {
do {
self.engine = try CHHapticEngine()
self.engine!.playsHapticsOnly = true
self.engine!.stoppedHandler = { [weak self] reason in
Log.debug("CHHapticEngine stop handler: The engine stopped for reason: \(reason.rawValue)")
self?.engineNeedsStart = true
}
self.engine!.resetHandler = { [weak self] in
Log.debug("Reset Handler: Restarting the engine.")
do {
// Try restarting the engine.
try self?.engine?.start()

// Indicate that the next time the app requires a haptic, the app doesn't need to call engine.start().
self?.engineNeedsStart = false

} catch {
Log.error("Failed to start the engine with error: \(error)")
}
}
} catch {
Log.error("CHHapticEngine error: \(error)")
}
} catch {
Log.error("CHHapticEngine error: \(error)")
}
}

Expand Down
Loading