Skip to content

Commit 80b8ff6

Browse files
author
Donald
committed
fix(ios): wrap the async functions at the entry point
1 parent f219db1 commit 80b8ff6

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed

ios/DeviceUpgrade.swift

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,38 @@ class DeviceUpgrade {
3535
}
3636

3737
func startUpgrade(resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
38-
DispatchQueue.main.async {
39-
self.upgradeResolver = resolve
40-
self.upgradeRejecter = reject
41-
42-
guard let bleUuid = UUID(uuidString: self.bleId) else {
43-
let error = NSError(domain: "", code: 200, userInfo: nil)
44-
return reject("error", "failed to parse uuid", error);
45-
}
46-
47-
guard let fileUrl = URL(string: self.fileURI) else {
48-
let error = NSError(domain: "", code: 200, userInfo: nil)
49-
return reject("error", "failed to parse file uri as url", error);
50-
}
51-
52-
do {
53-
let filehandle = try FileHandle(forReadingFrom: fileUrl)
54-
let file = Data(filehandle.availableData)
55-
filehandle.closeFile()
56-
57-
self.bleTransport = McuMgrBleTransport(bleUuid)
58-
self.dfuManager = FirmwareUpgradeManager(transporter: self.bleTransport!, delegate: self)
59-
60-
let estimatedSwapTime: TimeInterval = self.options["estimatedSwapTime"] as! TimeInterval
61-
let config = FirmwareUpgradeConfiguration(
62-
estimatedSwapTime: estimatedSwapTime
63-
)
64-
65-
self.dfuManager!.logDelegate = self.logDelegate
66-
self.dfuManager!.mode = self.getMode();
67-
68-
try self.dfuManager!.start(data: file as Data, using: config)
69-
} catch {
70-
reject(error.localizedDescription, error.localizedDescription, error)
71-
}
38+
self.upgradeResolver = resolve
39+
self.upgradeRejecter = reject
40+
41+
guard let bleUuid = UUID(uuidString: self.bleId) else {
42+
let error = NSError(domain: "", code: 200, userInfo: nil)
43+
return reject("error", "failed to parse uuid", error);
44+
}
45+
46+
guard let fileUrl = URL(string: self.fileURI) else {
47+
let error = NSError(domain: "", code: 200, userInfo: nil)
48+
return reject("error", "failed to parse file uri as url", error);
49+
}
50+
51+
do {
52+
let filehandle = try FileHandle(forReadingFrom: fileUrl)
53+
let file = Data(filehandle.availableData)
54+
filehandle.closeFile()
55+
56+
self.bleTransport = McuMgrBleTransport(bleUuid)
57+
self.dfuManager = FirmwareUpgradeManager(transporter: self.bleTransport!, delegate: self)
58+
59+
let estimatedSwapTime: TimeInterval = self.options["estimatedSwapTime"] as! TimeInterval
60+
let config = FirmwareUpgradeConfiguration(
61+
estimatedSwapTime: estimatedSwapTime
62+
)
63+
64+
self.dfuManager!.logDelegate = self.logDelegate
65+
self.dfuManager!.mode = self.getMode();
66+
67+
try self.dfuManager!.start(data: file as Data, using: config)
68+
} catch {
69+
reject(error.localizedDescription, error.localizedDescription, error)
7270
}
7371
}
7472

ios/McuManager.swift

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,26 @@ class RNMcuManager: RCTEventEmitter {
2222

2323
@objc
2424
func eraseImage(_ bleId: String, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
25-
guard let bleUuid = UUID(uuidString: bleId) else {
26-
let error = NSError(domain: "", code: 200, userInfo: nil)
27-
return reject("error", "failed to parse uuid", error);
28-
}
25+
DispatchQueue.main.async {
26+
guard let bleUuid = UUID(uuidString: bleId) else {
27+
let error = NSError(domain: "", code: 200, userInfo: nil)
28+
return reject("error", "failed to parse uuid", error);
29+
}
30+
31+
let bleTransport = McuMgrBleTransport(bleUuid)
32+
let imageManager = ImageManager(transporter: bleTransport)
2933

30-
let bleTransport = McuMgrBleTransport(bleUuid)
31-
let imageManager = ImageManager(transporter: bleTransport)
34+
imageManager.erase { (response: McuMgrResponse?, err: Error?) in
35+
bleTransport.close()
3236

33-
imageManager.erase { (response: McuMgrResponse?, err: Error?) in
34-
bleTransport.close()
37+
if (err != nil) {
38+
reject("ERASE_ERR", err?.localizedDescription, err)
39+
return
40+
}
3541

36-
if (err != nil) {
37-
reject("ERASE_ERR", err?.localizedDescription, err)
42+
resolve(nil)
3843
return
3944
}
40-
41-
resolve(nil)
42-
return
4345
}
4446
}
4547

@@ -50,12 +52,14 @@ class RNMcuManager: RCTEventEmitter {
5052

5153
@objc
5254
func runUpgrade(_ id: String, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
53-
guard let upgrade = self.upgrades[id] else {
54-
reject("ID_NOT_FOUND", "Upgrade object not found", nil)
55-
return
56-
}
55+
DispatchQueue.main.async {
56+
guard let upgrade = self.upgrades[id] else {
57+
reject("ID_NOT_FOUND", "Upgrade object not found", nil)
58+
return
59+
}
5760

58-
upgrade.startUpgrade(resolver: resolve, rejecter: reject)
61+
upgrade.startUpgrade(resolver: resolve, rejecter: reject)
62+
}
5963
}
6064

6165
@objc

0 commit comments

Comments
 (0)