How to pass potential error between methods
I'm trying to wrap Core Bluetooth Peripheral methods for use in React Native. It's a counterpart for already finished android code, so the API is set.
When I'm calling CBPeripheralManager.addService, I need to fulfill or reject a promise, handed from the javascript side.
The problem is, Core Bluetooth doesn't offer a callback for the method, it seems to expect private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?)
I'm new to iOS and Swift so this behavior seems strange to me. Any ideas how can I wrap the function so I can handle the error reporting properly?
Thanks
class BLE: NSObject, CBPeripheralManagerDelegate {
var advertising: Bool = false
var servicesMap = Dictionary<String, CBMutableService>()
var manager: CBPeripheralManager!
override init() {
super.init()
manager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
func addService(promise, serviceUUID) {
let serviceUUID = CBUUID(string: uuid)
let service = CBMutableService(type: serviceUUID, primary: true)
manager.add(service)
}
private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?) {
if let error = error {
// this should reject the addService promise
return
}
// this should fulfill the promise
}
}
swift core-bluetooth
|
show 1 more comment
I'm trying to wrap Core Bluetooth Peripheral methods for use in React Native. It's a counterpart for already finished android code, so the API is set.
When I'm calling CBPeripheralManager.addService, I need to fulfill or reject a promise, handed from the javascript side.
The problem is, Core Bluetooth doesn't offer a callback for the method, it seems to expect private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?)
I'm new to iOS and Swift so this behavior seems strange to me. Any ideas how can I wrap the function so I can handle the error reporting properly?
Thanks
class BLE: NSObject, CBPeripheralManagerDelegate {
var advertising: Bool = false
var servicesMap = Dictionary<String, CBMutableService>()
var manager: CBPeripheralManager!
override init() {
super.init()
manager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
func addService(promise, serviceUUID) {
let serviceUUID = CBUUID(string: uuid)
let service = CBMutableService(type: serviceUUID, primary: true)
manager.add(service)
}
private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?) {
if let error = error {
// this should reject the addService promise
return
}
// this should fulfill the promise
}
}
swift core-bluetooth
What do you want it to do when the error happens, exactly? Whatever that is, just do it inside theif let
block (or call a function from there to handle it and pass the error as a parameter).
– John Montgomery
Jan 3 at 0:34
1
This delegate method is incorrect, and is probably never called. The correct method would befunc peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
, and it can't be aprivate
method. TheaddService()
method also isn't valid, but in any case, you would need to store the promise somewhere so you can later fulfill it.
– Rob Napier
Jan 3 at 1:15
OK, thanks. But I'm confused, the CBPeripheralManagerDelegate class doesn't have access to it's private functions?
– Eskel
Jan 3 at 19:16
1
The class has access to its own private methods, but CBPeripheralManager doesn't have access to its delegate's private methods. It's the manager that calls this method, not the delegate.
– Rob Napier
Jan 3 at 19:24
1
@Eskel Correct. That's the name of the signature I wrote:func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
. The name of that isperipheralManager(_:didAdd:error:)
.
– Rob Napier
Jan 4 at 21:46
|
show 1 more comment
I'm trying to wrap Core Bluetooth Peripheral methods for use in React Native. It's a counterpart for already finished android code, so the API is set.
When I'm calling CBPeripheralManager.addService, I need to fulfill or reject a promise, handed from the javascript side.
The problem is, Core Bluetooth doesn't offer a callback for the method, it seems to expect private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?)
I'm new to iOS and Swift so this behavior seems strange to me. Any ideas how can I wrap the function so I can handle the error reporting properly?
Thanks
class BLE: NSObject, CBPeripheralManagerDelegate {
var advertising: Bool = false
var servicesMap = Dictionary<String, CBMutableService>()
var manager: CBPeripheralManager!
override init() {
super.init()
manager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
func addService(promise, serviceUUID) {
let serviceUUID = CBUUID(string: uuid)
let service = CBMutableService(type: serviceUUID, primary: true)
manager.add(service)
}
private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?) {
if let error = error {
// this should reject the addService promise
return
}
// this should fulfill the promise
}
}
swift core-bluetooth
I'm trying to wrap Core Bluetooth Peripheral methods for use in React Native. It's a counterpart for already finished android code, so the API is set.
When I'm calling CBPeripheralManager.addService, I need to fulfill or reject a promise, handed from the javascript side.
The problem is, Core Bluetooth doesn't offer a callback for the method, it seems to expect private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?)
I'm new to iOS and Swift so this behavior seems strange to me. Any ideas how can I wrap the function so I can handle the error reporting properly?
Thanks
class BLE: NSObject, CBPeripheralManagerDelegate {
var advertising: Bool = false
var servicesMap = Dictionary<String, CBMutableService>()
var manager: CBPeripheralManager!
override init() {
super.init()
manager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
func addService(promise, serviceUUID) {
let serviceUUID = CBUUID(string: uuid)
let service = CBMutableService(type: serviceUUID, primary: true)
manager.add(service)
}
private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?) {
if let error = error {
// this should reject the addService promise
return
}
// this should fulfill the promise
}
}
swift core-bluetooth
swift core-bluetooth
asked Jan 3 at 0:28
EskelEskel
4801317
4801317
What do you want it to do when the error happens, exactly? Whatever that is, just do it inside theif let
block (or call a function from there to handle it and pass the error as a parameter).
– John Montgomery
Jan 3 at 0:34
1
This delegate method is incorrect, and is probably never called. The correct method would befunc peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
, and it can't be aprivate
method. TheaddService()
method also isn't valid, but in any case, you would need to store the promise somewhere so you can later fulfill it.
– Rob Napier
Jan 3 at 1:15
OK, thanks. But I'm confused, the CBPeripheralManagerDelegate class doesn't have access to it's private functions?
– Eskel
Jan 3 at 19:16
1
The class has access to its own private methods, but CBPeripheralManager doesn't have access to its delegate's private methods. It's the manager that calls this method, not the delegate.
– Rob Napier
Jan 3 at 19:24
1
@Eskel Correct. That's the name of the signature I wrote:func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
. The name of that isperipheralManager(_:didAdd:error:)
.
– Rob Napier
Jan 4 at 21:46
|
show 1 more comment
What do you want it to do when the error happens, exactly? Whatever that is, just do it inside theif let
block (or call a function from there to handle it and pass the error as a parameter).
– John Montgomery
Jan 3 at 0:34
1
This delegate method is incorrect, and is probably never called. The correct method would befunc peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
, and it can't be aprivate
method. TheaddService()
method also isn't valid, but in any case, you would need to store the promise somewhere so you can later fulfill it.
– Rob Napier
Jan 3 at 1:15
OK, thanks. But I'm confused, the CBPeripheralManagerDelegate class doesn't have access to it's private functions?
– Eskel
Jan 3 at 19:16
1
The class has access to its own private methods, but CBPeripheralManager doesn't have access to its delegate's private methods. It's the manager that calls this method, not the delegate.
– Rob Napier
Jan 3 at 19:24
1
@Eskel Correct. That's the name of the signature I wrote:func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
. The name of that isperipheralManager(_:didAdd:error:)
.
– Rob Napier
Jan 4 at 21:46
What do you want it to do when the error happens, exactly? Whatever that is, just do it inside the
if let
block (or call a function from there to handle it and pass the error as a parameter).– John Montgomery
Jan 3 at 0:34
What do you want it to do when the error happens, exactly? Whatever that is, just do it inside the
if let
block (or call a function from there to handle it and pass the error as a parameter).– John Montgomery
Jan 3 at 0:34
1
1
This delegate method is incorrect, and is probably never called. The correct method would be
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
, and it can't be a private
method. The addService()
method also isn't valid, but in any case, you would need to store the promise somewhere so you can later fulfill it.– Rob Napier
Jan 3 at 1:15
This delegate method is incorrect, and is probably never called. The correct method would be
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
, and it can't be a private
method. The addService()
method also isn't valid, but in any case, you would need to store the promise somewhere so you can later fulfill it.– Rob Napier
Jan 3 at 1:15
OK, thanks. But I'm confused, the CBPeripheralManagerDelegate class doesn't have access to it's private functions?
– Eskel
Jan 3 at 19:16
OK, thanks. But I'm confused, the CBPeripheralManagerDelegate class doesn't have access to it's private functions?
– Eskel
Jan 3 at 19:16
1
1
The class has access to its own private methods, but CBPeripheralManager doesn't have access to its delegate's private methods. It's the manager that calls this method, not the delegate.
– Rob Napier
Jan 3 at 19:24
The class has access to its own private methods, but CBPeripheralManager doesn't have access to its delegate's private methods. It's the manager that calls this method, not the delegate.
– Rob Napier
Jan 3 at 19:24
1
1
@Eskel Correct. That's the name of the signature I wrote:
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
. The name of that is peripheralManager(_:didAdd:error:)
.– Rob Napier
Jan 4 at 21:46
@Eskel Correct. That's the name of the signature I wrote:
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
. The name of that is peripheralManager(_:didAdd:error:)
.– Rob Napier
Jan 4 at 21:46
|
show 1 more comment
1 Answer
1
active
oldest
votes
It's unclear what the type of promise
is, but you'll need to store it somewhere, and then fulfill it later. For example, you might add a property:
var pendingServices: [CBUUID: Promise] = [:]
(I don't know what you're promise type really is here)
Then you'd store it in addService
:
assert(pendingServices[serviceUUID] == nil)
pendingServices[serviceUUID] = promise
And later in (the correct; see my comment) delegate method, you'd deal with it:
if let promise = pendingServices.removeValue(forKey: service.uuid) {
promise.fulfill() // Or whatever you do with it
}
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014917%2fhow-to-pass-potential-error-between-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's unclear what the type of promise
is, but you'll need to store it somewhere, and then fulfill it later. For example, you might add a property:
var pendingServices: [CBUUID: Promise] = [:]
(I don't know what you're promise type really is here)
Then you'd store it in addService
:
assert(pendingServices[serviceUUID] == nil)
pendingServices[serviceUUID] = promise
And later in (the correct; see my comment) delegate method, you'd deal with it:
if let promise = pendingServices.removeValue(forKey: service.uuid) {
promise.fulfill() // Or whatever you do with it
}
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
add a comment |
It's unclear what the type of promise
is, but you'll need to store it somewhere, and then fulfill it later. For example, you might add a property:
var pendingServices: [CBUUID: Promise] = [:]
(I don't know what you're promise type really is here)
Then you'd store it in addService
:
assert(pendingServices[serviceUUID] == nil)
pendingServices[serviceUUID] = promise
And later in (the correct; see my comment) delegate method, you'd deal with it:
if let promise = pendingServices.removeValue(forKey: service.uuid) {
promise.fulfill() // Or whatever you do with it
}
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
add a comment |
It's unclear what the type of promise
is, but you'll need to store it somewhere, and then fulfill it later. For example, you might add a property:
var pendingServices: [CBUUID: Promise] = [:]
(I don't know what you're promise type really is here)
Then you'd store it in addService
:
assert(pendingServices[serviceUUID] == nil)
pendingServices[serviceUUID] = promise
And later in (the correct; see my comment) delegate method, you'd deal with it:
if let promise = pendingServices.removeValue(forKey: service.uuid) {
promise.fulfill() // Or whatever you do with it
}
It's unclear what the type of promise
is, but you'll need to store it somewhere, and then fulfill it later. For example, you might add a property:
var pendingServices: [CBUUID: Promise] = [:]
(I don't know what you're promise type really is here)
Then you'd store it in addService
:
assert(pendingServices[serviceUUID] == nil)
pendingServices[serviceUUID] = promise
And later in (the correct; see my comment) delegate method, you'd deal with it:
if let promise = pendingServices.removeValue(forKey: service.uuid) {
promise.fulfill() // Or whatever you do with it
}
answered Jan 3 at 1:19
Rob NapierRob Napier
206k28305433
206k28305433
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
add a comment |
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
Ah, I see. Seems a little complicated but I guess it's the only way. Too bad apple doesn't provide a way to use a my own custom function. Anyway, thank you!
– Eskel
Jan 3 at 19:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014917%2fhow-to-pass-potential-error-between-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you want it to do when the error happens, exactly? Whatever that is, just do it inside the
if let
block (or call a function from there to handle it and pass the error as a parameter).– John Montgomery
Jan 3 at 0:34
1
This delegate method is incorrect, and is probably never called. The correct method would be
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
, and it can't be aprivate
method. TheaddService()
method also isn't valid, but in any case, you would need to store the promise somewhere so you can later fulfill it.– Rob Napier
Jan 3 at 1:15
OK, thanks. But I'm confused, the CBPeripheralManagerDelegate class doesn't have access to it's private functions?
– Eskel
Jan 3 at 19:16
1
The class has access to its own private methods, but CBPeripheralManager doesn't have access to its delegate's private methods. It's the manager that calls this method, not the delegate.
– Rob Napier
Jan 3 at 19:24
1
@Eskel Correct. That's the name of the signature I wrote:
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
. The name of that isperipheralManager(_:didAdd:error:)
.– Rob Napier
Jan 4 at 21:46