Git Product home page Git Product logo

Comments (4)

Tricertops avatar Tricertops commented on June 8, 2024

No, there is not. Initial value is desired in all cases I have ever encountered. Why do you need it?

You can skip the first by using __block counter outside of the the observation.

from block-kvo.

zdennis avatar zdennis commented on June 8, 2024

@iMartinKiss, I would like to disable initial value observation for an object that represents a remote device. This object is KVO'd for updating values on the remote device. However, the initial values of nil and 0 really can be ignored as they shouldn't be written back to the device. Currently, I have to add a check around every single property of that remote device to make sure the initial value doesn't get sent to the remote device.

If observation without the initial value existed then I could remove a bunch of unnecessary conditional checks and rely on my own manual KVO triggering after the properties of the remote object had loaded. I already disable automatic KVO but the initial value observation is still a problem.

from block-kvo.

Tricertops avatar Tricertops commented on June 8, 2024

Few weeks ago I was also working with remote devices and similarily sending KVO changes through MCSession, but I had no problem with initial values. I had two cases:

  1. At the moment of registering observation the device was not yet connected, so attempt to send initial value was ignored.
  2. At the moment of registering observation the device was connected and there already was a valid value in property. This way the device immediately received existing value, not just future changes.

However, I used coalescing for these updates, so they were sent in batches and I used nil as unknown value, so it indicated nothing was received yet (on the remote device).

Anyway you can simply create a wrapping method that chacks for initial calback:

- (void)observePropertyWithoutInitialValue:(NSString *)keyPath withBlock:(MTKBlockChange)observationBlock {
    __block BOOL isInitial = YES;
    [self observeProperty:keyPath withBlock:^(__weak id weakSelf, id old, id new) {
        if (isInitial) {
            isInitial = NO;
        }
        else {
            observationBlock(weakSelf, old, new);
        }
    }];
}

from block-kvo.

zdennis avatar zdennis commented on June 8, 2024

Thanks for the reply @iMartinKiss. I am currently using an uglier version of your suggested wrapper method, so I'll be updating that. Any chance of that being added to Block-KVO? That seems like it should be a part of the library rather than people piecemeal adding it.

Regarding 1 above, I'm worried about having a coincidental dependency in code where it happens to work out because the device isn't connected. I'm not sure this will always be the case. Need to think about this more.

Regarding 2 above, unfortunately, it's likely to not be true in my case where valid values exist upon connection.

Thanks again, Block-KVO has been a wonderful help.

from block-kvo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.