rightpoint / rzdatabinding Goto Github PK
View Code? Open in Web Editor NEWLightweight KVO-based data binding options.
License: Other
Lightweight KVO-based data binding options.
License: Other
[!] The platform of the target Pods
(OS X 10.8) is not compatible with RZDataBinding (2.0.0)
, which does not support osx
.
Xcode Version 11.4
13.4 iOS Simulator iPad Pro 9.7"
The documentation for bindings (both in the readme and in the header docs) is a little hard to follow, because it's hard to keep track of various receivers and keys and bindings in the abstract. A concrete example would be helpful.
Since you have unit tests, pull requests should be built on Travis to ensure that tests and static analysis pass. I recommend editing the default Travis script to run on iPhone 4s and iPhone 6 to catch 32/64 issues.
The current version requires that you pass in the current class explicitly if you need to pass the class of self
. There should be an easier/prettier way to do this.
I'm subclassing NSLayoutConstraint
, and I want to know when the firstItem
’s font
changes. So I set it up like this:
- (void)configureObservers
{
NSString *firstItemFontKeyPath = [@[
NSStringFromSelector(@selector(firstItem)),
NSStringFromSelector(@selector(font)),
] componentsJoinedByString:@"."];
[self rz_addTarget:self
action:@selector(updateConstant)
forKeyPathChange:firstItemFontKeyPath
callImmediately:YES];
}
Notice that I'm building the key path myself. This is because firstItem
is of type id
, and depending on what type of view it is, it may not have a font
property.
If I run this code as-is, I get a crash:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DashedHairlineView 0x7fdaa492cbc0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key font.'
If I delete the callImmediately
parameter and call the handler myself on the next line, it works fine, and subsequent changes successfully trigger the callback.
Misleading typo in documentation for RZDB_AUTOMATIC_CLEANUP
The comment preceding the definition of RZDB_AUTOMATIC_CLEANUP
reads:
If enabled, it is safe to observe or bind to weak references, and there is need to call rz_removeTarget or rz_unbindKey before targets or observed objects are deallocated.
Expected: Text should read "… there is no need …"
Actual: Text reads "there is need"
Full comment below:
* Set this to 1 (recommended) to enable automatic cleanup of observers on object deallocation.
* If enabled, it is safe to observe or bind to weak references, and there is need to call rz_removeTarget
* or rz_unbindKey before targets or observed objects are deallocated. To achieve automatic cleanup,
* RZDB swizzles the dealloc method to ensure observers are properly invalidated. There are other ways of implementing similar
* behavior, but this has been found to be both the safest and most reliable in production.
*
* If set to 0 (not recommended), objects MUST remove themselves as targets and unbind their keys from any observed objects before being deallocated.
* Failure to do so will result in crashes (just like standard KVO). Additionally, you should not add a target to or bind keys to
* objects without first establishing a strong reference. Otherwise, the foreign object might be deallocated before the observer, causing in a crash.
* If you choose to disable global automatic cleanup by setting this to 0, you must cleanup observers manually.
*
* @see rz_cleanupObservers
*/
I find myself writing this a lot:
+ (NSSet *)keyPathsForValuesAffectingMyKeyPath
{
return [NSSet setWithObjects:
RZDB_KP(ClassName, self.key.path.1),
RZDB_KP(ClassNmae, self.key.path.2),
nil];
}
It would be awesome to have a macro that would:
RZDB_KEYPATHS_AFFECTING( MyKeyPath, ClassName, self.key.path.1, self.key.path.2)
I try make the binding like this:
[self.company rz_bindKey:RZDB_KP(MMCompanyItem, contractName) toKeyPath:RZDB_KP(UITextField, text) ofObject:self.txContractName];
Doesn't works when change the value of UITextField, the property contractName on the object self.company doesn't reflects the change.
Am I doing something wrong?
Hi, thank you for a great library, I really enjoy using it.
Recently, I've been receiving tens of crashes on [RZDBObserver invalidate]
with an exception *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 189 beyond bounds [0 .. 188]'
(this reason keeps repeating, but with different numbers like index 605 beyond bounds [0 .. 604]'
.
Last Exception Backtrace:
0 CoreFoundation 0x27a29c1f 0x27924000 + 1072159
1 libobjc.A.dylib 0x35210c8b 0x3520a000 + 27787
2 CoreFoundation 0x2793e839 0x27924000 + 108601
3 CoreFoundation 0x27956b77 0x27924000 + 207735
4 RZDataBinding 0x00fbd0fd -[RZDBObserver invalidate] (NSObject+RZDataBinding.m:391)
5 CoreFoundation 0x279512df 0x27924000 + 185055
6 CoreFoundation 0x27951209 0x27924000 + 184841
7 RZDataBinding 0x00fbc9ad -[NSObject(RZDataBinding_Private) rz_cleanupObservers] (NSObject+RZDataBinding.m:308)
8 RZDataBinding 0x00fbd799 __rz_swizzleDeallocIfNeeded_block_invoke_2 (NSObject+RZDataBinding.m:496)
9 libobjc.A.dylib 0x3522ad5f 0x3520a000 + 134495
10 CoreFoundation 0x27930e5d 0x27924000 + 52829
11 CoreFoundation 0x279444e7 0x27924000 + 132327
12 libobjc.A.dylib 0x3522ad5f 0x3520a000 + 134495
Do you have any idea where the problem could be?
Thanks!
I think this method should have a variant that takes callImmediately
parameter, like its single-key-path cousin.
Thank you for your great work!
Is it possible to convert action-target usage to blocks?
If rz_bind
is used to bind to a primitive value, the withFunction:
version must be used to properly handle nil
values.
I am super stoked about this library. Data binding is something that is sorely missing from Cocoa and I am thrilled that RZ is putting this out there.
That said, swizzling dealloc sort of scares me. I see why it's being done, and it's good that you made it optional via the preprocessor, but at the same time I think it should be strongly discouraged for production apps.
Is there any other way to clean up observers besides subclassing or swizzling dealloc for all NSObjects?
The let side of the parameters are all aligned, but if you let Xcode auto-indent the code, colons on subsequent lines will be aligned instead. I recommend using this style for the readme because it is more familiar.
Current rz_addTarget:action:
always uses at least NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
. These options should only be used if the action's method signature has a changeDict parameter. In the case that the action takes no arguments, kNilOptions
should be used to avoid this overhead.
Will it support tvOS soon?
I was using version 1.2, and just updated to 2.0. I noticed it included a breaking change, and won't compile now, but can't find any documentation on it.
The code I had looked like this
[self rz_addTarget:self action:@selector(reloadAppIntegrations) forKeyPathChange:RZDB_KP(self.tableDataSource.appIntegrations) callImmediately:NO];
It looks like the RZDB_KP
was removed, so I'm wondering how do I pass in a keyPath (preferably not as a string?) to create the binding on.
Thanks a lot!
If a property is set to the same value 5 times, only 1 set value should occur.
Thanks for your contribution.I have a question.Do you delete some profile? I can not run it in my X-code
Initial naming was unclear, RZDBKeyBindingTransform
would make more sense, since it is a block parameter and not a function pointer.
Makes more sense to expose rz_cleanupObservers
than enforcing a subclassing hierarchy.
I'm trying to use the RZDataBinding library to observe changes in a property of a NSObject. I have the following code:
[self rz_addTarget:self action:@selector(senderDidChange:) forKeyPathChange:RZDB_KP_SELF(sender)];
It compiles, but when it tries to run it throws an error of unrecognised selector being sent to the instance. The self
object is a direct subclass of NSObject.
Any idea what is going wrong here?
I'd like to add this:
#if DEBUG
#define RZDB_KP_PROTOCOL(ProtocolName, keypath) ({\
id<ProtocolName> _rzdb_keypath_obj; \
__unused __typeof(_rzdb_keypath_obj.keypath) _rzdb_keypath_prop; \
@#keypath; \
})
#else
#define RZDB_KP_PROTOCOL(ProtocolName, keypath) (@#keypath)
#endif
I don't know if this belongs in RZDB directly, but it came up recently in a project that's using RZDB anyway, so I already have access to the other key path macros:
+ (void)abc_importResponse:(id)responseObject inContext:(NSManagedObjectContext *)context
{
[self rzi_objectsFromArray:responseObject inContext:context];
if ([self conformsToProtocol:@protocol(ABCDeletableStoredObject)]) {
[self rzv_deleteAllWhere:[NSPredicate predicateWithFormat:@"%K == %@", RZDB_KP_PROTOCOL(ABCDeletableStoredObject, flaggedForDelete), @(YES)] inContext:context];
}
}
Recommend using these warnings for the sample project:
-Weverything -Wno-objc-missing-property-synthesis -Wno-objc-property-synthesis -Wno-direct-ivar-access -Wno-assign-enum -Wno-float-equal -Wno-sign-compare -Wno-switch-enum -Wno-gnu -Wnewline-eof
Investigate a pure Swift version of the library.
I can see that it makes sense to call the function immediately in the setup process, but I ran into some cases where it would make sense to define wether the function should be called during the setup process or if the first function call should be defered until the real first change was triggered by KVO.
Any thoughts on that?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.