Skip to content

Commit d54c2bc

Browse files
committed
Update AndroidMainActor
1 parent 35068f3 commit d54c2bc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Sources/AndroidLooper/AndroidMainActor.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public actor AndroidMainActor: GlobalActor {
2121
public static let shared = AndroidMainActor()
2222

2323
public static let sharedUnownedExecutor: UnownedSerialExecutor = {
24+
// ensure executor is retained to avoid crash
25+
// https://forums.swift.org/t/how-to-properly-use-custom-executor-on-global-actor/71829/4
2426
guard let executor = AndroidMainActor.executor else {
2527
fatalError("Executor was never installed")
2628
}
@@ -57,7 +59,7 @@ public extension AndroidMainActor {
5759
executor = try Looper.Executor(looper: looper)
5860
}
5961
catch {
60-
assertionFailure("Unable to initialize \(String(describing: Looper.Executor.self)): \(error)")
62+
assertionFailure("Unable to initialize Looper.Executor: \(error)")
6163
return false
6264
}
6365

@@ -132,6 +134,7 @@ private extension AndroidMainActor {
132134
}
133135
// install executor
134136
self.executor = executor
137+
_ = mainLoop
135138
return true
136139
}
137140
}
@@ -146,12 +149,12 @@ internal extension Looper.Handle {
146149
var outData: UnsafeMutableRawPointer?
147150

148151
let timeoutMillis: CInt
149-
if let duration = duration {
150-
timeoutMillis = CInt(Double(duration.components.seconds) * 1000 + Double(duration.components.attoseconds) * 1e-15)
152+
if let duration {
153+
timeoutMillis = CInt(duration.milliseconds)
151154
} else {
152155
timeoutMillis = 0
153156
}
154-
157+
155158
let err = ALooper_pollOnce(timeoutMillis, &outFd, &outEvents, &outData)
156159
switch Int(err) {
157160
case ALOOPER_POLL_WAKE:

0 commit comments

Comments
 (0)