How to pass potential error between methods












0















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
}
}









share|improve this question























  • 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 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






  • 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 is peripheralManager(_:didAdd:error:).

    – Rob Napier
    Jan 4 at 21:46


















0















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
}
}









share|improve this question























  • 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 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






  • 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 is peripheralManager(_:didAdd:error:).

    – Rob Napier
    Jan 4 at 21:46
















0












0








0








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
}
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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 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






  • 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 is peripheralManager(_: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






  • 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











  • 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 is peripheralManager(_: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














1 Answer
1






active

oldest

votes


















0














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
}





share|improve this answer
























  • 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














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
});


}
});














draft saved

draft discarded


















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









0














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
}





share|improve this answer
























  • 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


















0














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
}





share|improve this answer
























  • 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
















0












0








0







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
}





share|improve this answer













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
}






share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith