Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBCharacteristic update value may lose due to asynchronous call on main queue #100

Open
coupgar opened this issue Feb 18, 2014 · 7 comments

Comments

@coupgar
Copy link
Contributor

coupgar commented Feb 18, 2014

In the cbperiphral delegate callback, i.e. peripheral:didUpdateValueForCharacteristic:error, you call the notifyCharacteristicHandler:error handler on the main queue, however, the value of CBCharacteristic may have changed by the time main queue come back.

Other call back on the main queue may have same issue. related file is YMSCBPeripheral.m

@kickingvegas
Copy link
Owner

Will investigate.

@kickingvegas
Copy link
Owner

FWIW, there are a whole host of changes coming to callback handling when I get the cycles to put them in.

@jaysonjh
Copy link

i hit the same issue,i moved the main queue code to delegate code line:
__weak YMSCBPeripheral *this = self;
YMSCBService *btService = [this findService:characteristic.service];
YMSCBCharacteristic *yc = [btService findCharacteristic:characteristic];
if (yc.cbCharacteristic.isNotifying) {
[btService notifyCharacteristicHandler:yc error:error];

} else {
    if ([yc.readCallbacks count] > 0) {
        [yc executeReadCallback:characteristic.value error:error];
    }
}
_YMS_PERFORM_ON_MAIN_THREAD(^{
    if ([this.delegate respondsToSelector:@selector(peripheral:didUpdateValueForCharacteristic:error:)]) {
        [this.delegate peripheral:peripheral didUpdateValueForCharacteristic:characteristic error:error];
    }
});

And then update value is ok.

@jaysonjh
Copy link

YmsCoreBluetooth lib stop updated?

@kickingvegas
Copy link
Owner

@jaysonjh Nope, but been put into a longer hiatus than I expected due to work issues. There's a lot I'd like to change given my working with this codebase for some time. In particular, the changes made with respect to this issue is recognizing that YMSCoreBluetooth is trying too hard to push stuff back on the main queue in the delegate methods. Changing the code base to stop doing this will definitely break existing installs and when that happens, then all kinds of design decisions in this framework are open for review.

@jimneylee
Copy link

Today I have the same problem, solved it just make thead sleep 0.01s. not a good way BUT minimum change.

    _YMS_PERFORM_ON_MAIN_THREAD(^{
      // ......
    });
    [NSThread sleepForTimeInterval:0.01];

@jimneylee
Copy link

when log the data, I found the reason:
didUpdateValueForCharacteristic
update data = <202d>
update data = <39000000 000000>
update data = <31780a>
update data = <3d0000>

in main thread, got the data as follow:
parse data = <3d0000>
parse data = <3d0000>
parse data = <3d0000>
parse data = <3d0000>

if in one cycle, received four values, then in main thread, they are same to last value(3d0000), I think the reason is that all the points of characteristic is pointing at last one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants