Skip to content

Commit

Permalink
fix(iOS): Removing runloop thread (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
theproducer authored Apr 18, 2024
1 parent e1b54f7 commit 11124d2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 53 deletions.
6 changes: 6 additions & 0 deletions .changeset/funny-bulldogs-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@capacitor/background-runner": patch
"ios-engine": patch
---

(iOS): Removing unnecessary run loop thread causing spike in CPU while the app was in the foreground
3 changes: 1 addition & 2 deletions packages/capacitor-plugin/ios/Plugin/BackgroundRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class BackgroundRunner {

public init() {
do {
config = try self.loadRunnerConfig()
runner.start()
config = try self.loadRunnerConfig()
} catch {
print("could not initialize BackgroundRunner: \(error)")
}
Expand Down
32 changes: 0 additions & 32 deletions packages/ios-engine/Sources/RunnerEngine/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,4 @@ public class Runner {
func createContext(name: String) throws -> Context {
return try Context(vm: machine, contextName: name, runLoop: runLoop)
}

func start() {
thread = Thread { [weak self] in
defer {
Thread.exit()
}

guard let self = self else { return }
guard let thread = self.thread else { return }

while !thread.isCancelled {
self.runLoop.run(mode: .default, before: .distantFuture)
}
}

thread?.qualityOfService = .userInitiated
thread?.start()
}

func stop() {
guard let thread = self.thread else {
return
}

thread.cancel()

while !thread.isFinished {
Thread.sleep(forTimeInterval: 0.05)
}

self.thread = nil
}
}
19 changes: 0 additions & 19 deletions packages/ios-engine/Tests/RunnerEngineTests/RunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,8 @@ import XCTest
@testable import RunnerEngine

final class RunnerTests: XCTestCase {
func testRunnerCreateDestroy() {
let runner = Runner()
runner.start()

runner.stop()
}

func testContextCreateDestroy() throws {
let runner = Runner()
runner.start()

_ = try runner.createContext(name: "io.ionic.android_js_engine")

runner.stop()
}

func testMultipleContexts() throws {
let runner = Runner()
runner.start()

var calls = 0
let count = Int.random(in: 20..<40)
Expand All @@ -43,8 +26,6 @@ final class RunnerTests: XCTestCase {
}

wait(for: [expectation], timeout: 5)

runner.stop()
}

}
Expand Down

0 comments on commit 11124d2

Please sign in to comment.