Skip to content

Commit

Permalink
Make sure watch extension code runs on main queue. (LoopKit#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 authored Feb 15, 2018
1 parent 48e1d2d commit 51f28ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,14 @@ final class AddCarbsInterfaceController: WKInterfaceController, IdentifiableClas
do {
try WCSession.default.sendCarbEntryMessage(entry,
replyHandler: { (suggestion) in
WKExtension.shared().rootInterfaceController?.presentController(withName: BolusInterfaceController.className, context: suggestion)
DispatchQueue.main.async {
WKExtension.shared().rootInterfaceController?.presentController(withName: BolusInterfaceController.className, context: suggestion)
}
},
errorHandler: { (error) in
ExtensionDelegate.shared().present(error)
DispatchQueue.main.async {
ExtensionDelegate.shared().present(error)
}
}
)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass {

do {
try WCSession.default.sendBolusMessage(bolus) { (error) in
ExtensionDelegate.shared().present(error)
DispatchQueue.main.async {
ExtensionDelegate.shared().present(error)
}
}
} catch {
presentAlert(
Expand Down
29 changes: 17 additions & 12 deletions WatchApp Extension/Controllers/StatusInterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,29 +244,34 @@ final class StatusInterfaceController: WKInterfaceController, ContextUpdatable {
do {
try WCSession.default.sendGlucoseRangeScheduleOverrideMessage(userInfo,
replyHandler: { _ in
self.pendingMessageResponses -= 1
if self.pendingMessageResponses == 0 {
self.updateForOverrideContext(userInfo?.context)
DispatchQueue.main.async {
self.pendingMessageResponses -= 1
if self.pendingMessageResponses == 0 {
self.updateForOverrideContext(userInfo?.context)
}
self.lastOverrideContext = userInfo?.context
}
self.lastOverrideContext = userInfo?.context
},
errorHandler: { error in
self.pendingMessageResponses -= 1
if self.pendingMessageResponses == 0 {
self.updateForOverrideContext(self.lastOverrideContext)
DispatchQueue.main.async {
self.pendingMessageResponses -= 1
if self.pendingMessageResponses == 0 {
self.updateForOverrideContext(self.lastOverrideContext)
}
ExtensionDelegate.shared().present(error)
}
ExtensionDelegate.shared().present(error)
}
)
} catch {
pendingMessageResponses -= 1
if pendingMessageResponses == 0 {
updateForOverrideContext(lastOverrideContext)
}
presentAlert(withTitle: NSLocalizedString("Send Failed", comment: "The title of the alert controller displayed after a glucose range override send attempt fails"),
message: NSLocalizedString("Make sure your iPhone is nearby and try again", comment: "The recovery message displayed after a glucose range override send attempt fails"),
preferredStyle: .alert,
actions: [WKAlertAction.dismissAction()]
presentAlert(
withTitle: NSLocalizedString("Send Failed", comment: "The title of the alert controller displayed after a glucose range override send attempt fails"),
message: NSLocalizedString("Make sure your iPhone is nearby and try again", comment: "The recovery message displayed after a glucose range override send attempt fails"),
preferredStyle: .alert,
actions: [WKAlertAction.dismissAction()]
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion WatchApp Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ final class ExtensionDelegate: NSObject, WKExtensionDelegate {

extension ExtensionDelegate: WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if activationState == .activated && lastContext == nil {
if activationState == .activated {
updateContext(session.receivedApplicationContext)
}
}
Expand Down

0 comments on commit 51f28ce

Please sign in to comment.