key-value-observing

Performance hit incurred using NSMutableDictionary vs. NSMutableArray>

一笑奈何 提交于 2019-12-06 05:36:49
问题 I am considering using an NSMutableDictionary in place of my current NSMutableArray. This is primarily for KVC/KVO reasons. The collection will undergo heavy mutation within the inner loop of my drawing method. Can I expect to incur a significant performance hit if I go ahead with this replacement? Cheers, Doug 回答1: The only way to be sure is to measure. None of us have enough knowledge about how NSMutableDictionary's and NSMutableArray's implementations work, so there's little point asking.

Best practice to remove an object as observer for some KVO property

筅森魡賤 提交于 2019-12-06 02:51:25
问题 I wanted to know what are the best practices for adding and removing self as observer for some KVO property. I have added my controller object as observer for "hidden" property of a UIView. I added the observer in loadView of my view controller. Now, what is the best place to DE-register as observer for this property. I want to stop observing as soon as the view controller's view is dismissed. At times I am seeing below console warnings and at times I am crashing due to over removal as

When using KVO is it necessary to remove self as an observer of self in -dealloc?

时光毁灭记忆、已成空白 提交于 2019-12-05 23:54:08
问题 in my NSObject subclass's -init method the instance adds itself as an observer of some of its own keyPaths in order to trigger an action that should occur any time one of the properties in question is changed. eg. [self addObserver:self forKeyPath:@"aProperty" options:0 context:nil]; [self addObserver:self forKeyPath:@"anotherProperty" options:0 context:nil]; ... My question is, in the class's -dealloc method do I necessarily need to remove the instance as an observer of itself? eg. [self

Using NSKeyValueObservation to observe value in UserDefaults

倾然丶 夕夏残阳落幕 提交于 2019-12-05 22:24:26
问题 I would like to use the block-based KVO from Swift 4 to observe changes to a value in UserDefaults . I am able to do this for observing a key path for WKWebView 's estimatedProgress but haven't been successful with UserDefaults because the provided key path isn't what it's looking for. Providing just a String isn't enough (Generic parameter 'Value' could not be inferred), prefixing it with \ isn't enough (Type of expression is ambiguous without more context). What's the correct way to create

KVO broken in iOS 9.3

两盒软妹~` 提交于 2019-12-05 21:28:56
This might be an awful bug in iOS 9.3 (release). When adding a single observer to [NSUserDefaults standardUserDefaults] I've noticed that the responding method -observeValueForKeyPath:ofObject:change:context: is called multiple times. In the simple example below, every time a UIButton is pressed once, observeValueForKeyPath fires twice. In more complicated examples it fires even more times. It is only present on iOS 9.3 (both on sim and devices). This can obviously wreak havoc on an app. Anyone else experiencing the same? // ViewController.m (barebones, single view app) - (void)viewDidLoad {

KVO for one-to-many but NSNull object passed into observeValueForKeyPath

和自甴很熟 提交于 2019-12-05 20:22:19
I have one managed object with a one-to-many relationship to member class. When I add the observers for members, it worked. When one new member is added to the relationship, the observeValueForKeyPath will be invoked with the new object and change dictionary contains the new member object. However, observeValueForKeyPath will be triggered second time with all values nil and change dictionary new="NULL". What is the second trigger? I set a breakpoint, but not sure who made the trigger. @interface FooObject : NSManagedObject {} @property (nonatomic, strong) NSString *fooId; @property (nonatomic,

Key Value Observing and NSButton state

自古美人都是妖i 提交于 2019-12-05 20:15:43
I'm trying to observe checkbox status and make appropriate changes in the app when checkbox status changes. In a window manager that manages the window with checkbox I have following observer setup: - (void)awakeFromNib { [myCheckBox addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL]; } - (void)dealloc { [myCheckBox removeObserver:self forKeyPath:@"state"]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"KeyPath: %@", keyPath); NSLog(@

AVPlayer removing observer crash in Swift 2.2

a 夏天 提交于 2019-12-05 18:58:38
I have a video app that I built a while back in Swift 1 and I've been trying to migrate to Swift 2.2. It all (finally) works apart from a weird crash to do with observers. func removeObservers() { print("REMOVING OBSERVERS") if ( !self.is_image && self.player != nil ) { if (self.player?.observationInfo != nil) { self.player?.removeObserver(self, forKeyPath: "currentItem.status") self.player?.removeObserver(self, forKeyPath: "readyForDisplay") } } NSNotificationCenter.defaultCenter().removeObserver(self) } This worked previously using SwiftTryCatch but with the lines in place crashes with "

What's a good way to bind from a shared utility window and the frontmost document window?

冷暖自知 提交于 2019-12-05 16:50:39
问题 I have an application which allows for multiple NSDocuments to be open. In this application is a single utility window that contains some functionality that I want to apply to the frontmost document. I am trying to use bindings here, so the trick is how to cleanly bind the user interface of the utility window to the frontmost document. The goal is that then switching the frontmost document window will update the view in the utility window; controls that are bound to properties of the

Why is my NSOperation subclass never finishing?

北战南征 提交于 2019-12-05 15:43:35
I have an NSOperation subclass that I want to run concurrently. My understanding is that for concurrent operations to work: I need to define isConcurrent to return YES . I need to define the start method I need to send KVOs notification for isExecuting and isFinished when it's done. Using @synthesize will automatically send the appropriate KVO notifications when the values for isExecuting and isFinished are changed. Despite this, I have verified that my queue never moves on to the next item. Here's the meat of my code: @interface MyOperation() @property (readwrite) BOOL isExecuting; @property