Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(539)

Unified Diff: ios/chrome/browser/ui/util/label_observer.mm

Issue 2828743002: LabelObserver is no longer retained by the label (Closed)
Patch Set: Address comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/chrome/browser/ui/util/label_observer.h ('k') | ios/chrome/browser/ui/util/label_observer_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/chrome/browser/ui/util/label_observer.mm
diff --git a/ios/chrome/browser/ui/util/label_observer.mm b/ios/chrome/browser/ui/util/label_observer.mm
index 4fa5c8477581690d2d482fee10afa4b06c17458c..06510b0f9b969c11ffdb23518c9e063bad2d6b0f 100644
--- a/ios/chrome/browser/ui/util/label_observer.mm
+++ b/ios/chrome/browser/ui/util/label_observer.mm
@@ -38,9 +38,19 @@ NSString* GetStringValue(id value) {
// property on |_label|.
@property(nonatomic, assign, getter=isRespondingToKVO) BOOL respondingToKVO;
+// The number of times this observer has been asked to observe the label. When
+// reaching zero, the label stops being observed.
+@property(nonatomic, assign) NSInteger observingCount;
+
// Initializes a LabelObserver that observes |label|.
- (instancetype)initWithLabel:(UILabel*)label NS_DESIGNATED_INITIALIZER;
+// Adds |self| as observer for the |_label|.
+- (void)registerAsObserver;
+
+// Removes |self| as observer for the |_label|.
+- (void)removeObserver;
+
// Performs all LabelObserverActions in |actions|.
- (void)performActions:(NSArray*)actions;
@@ -62,6 +72,7 @@ static NSSet* textKeys;
@implementation LabelObserver
@synthesize respondingToKVO = _respondingToKVO;
+@synthesize observingCount = _observingCount;
+ (void)initialize {
if (self == [LabelObserver class]) {
@@ -79,25 +90,14 @@ static NSSet* textKeys;
if ((self = [super init])) {
DCHECK(label);
_label.reset(label);
- for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) {
- for (NSString* key in keySet) {
- [_label addObserver:self
- forKeyPath:key
- options:NSKeyValueObservingOptionNew
- context:nullptr];
- }
- }
[self resetLabelAttributes];
}
return self;
}
- (void)dealloc {
- for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) {
- for (NSString* key in keySet) {
- [_label removeObserver:self forKeyPath:key];
- }
- }
+ objc_setAssociatedObject(_label, kLabelObserverKey, nil,
+ OBJC_ASSOCIATION_ASSIGN);
[super dealloc];
}
@@ -110,12 +110,29 @@ static NSSet* textKeys;
if (!observer) {
observer = [[LabelObserver alloc] initWithLabel:label];
objc_setAssociatedObject(label, kLabelObserverKey, observer,
- OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- [observer release];
+ OBJC_ASSOCIATION_ASSIGN);
+ [observer autorelease];
}
return observer;
}
+- (void)startObserving {
+ if (self.observingCount == 0) {
+ [self registerAsObserver];
+ }
+ self.observingCount++;
+}
+
+- (void)stopObserving {
+ if (self.observingCount == 0)
+ return;
+
+ self.observingCount--;
+ if (self.observingCount == 0) {
+ [self removeObserver];
+ }
+}
+
- (void)addStyleChangedAction:(LabelObserverAction)action {
DCHECK(action);
if (!_styleActions)
@@ -142,6 +159,25 @@ static NSSet* textKeys;
#pragma mark -
+- (void)registerAsObserver {
+ for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) {
+ for (NSString* key in keySet) {
+ [_label addObserver:self
+ forKeyPath:key
+ options:NSKeyValueObservingOptionNew
+ context:nullptr];
+ }
+ }
+}
+
+- (void)removeObserver {
+ for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) {
+ for (NSString* key in keySet) {
+ [_label removeObserver:self forKeyPath:key];
+ }
+ };
+}
+
- (void)performActions:(NSArray*)actions {
for (LabelObserverAction action in actions)
action(_label);
« no previous file with comments | « ios/chrome/browser/ui/util/label_observer.h ('k') | ios/chrome/browser/ui/util/label_observer_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698