Comments (4)
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.
@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.
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:
- At the moment of registering observation the device was not yet connected, so attempt to send initial value was ignored.
- 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.
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)
- Readme.md Is it necessary to remove the observer (like with regular KVO) or not? HOT 8
- Support removing observer for specified keypath
- Not compiling with Xcode 6.3, Swift 1.2 HOT 9
- Compile error when included in .mm file HOT 3
- Can't compile 'objc_msgSend has too many arguments' HOT 9
- Block-KVO logic should follow the same approach as original KVO HOT 1
- Macro MTKObservePropertySelf can not use with stringWithFormat in the block HOT 2
- MTKObserver should removeObserver in dealloc HOT 5
- can you delete "block(self, nil);" HOT 1
- Update deployment_target for cocoa pods HOT 1
- Expose change dictionary for observing collections HOT 2
- Obscure retain issue in -removeAllObservationsOfObject: HOT 10
- Registering change observer without calling block with initial value HOT 3
- Initial value detection HOT 4
- Two-way binding causing infinite loop HOT 2
- Too Many Arguments to Function Call HOT 3
- Update cocoapods version HOT 1
- Want my stuff HOT 2
- PodSpec Version HOT 1
- worth mentioning the use of @keypath in the example
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from block-kvo.