From bbeee95e6d6d933f589c830fbe2dc743d2fe5801 Mon Sep 17 00:00:00 2001 From: Marc Knaup Date: Thu, 23 Oct 2014 14:44:18 +0200 Subject: [PATCH 1/2] Fixed race condition in deferred link detection. --- src/NIAttributedLabel.m | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/NIAttributedLabel.m b/src/NIAttributedLabel.m index 16a0c8a..14c29c2 100644 --- a/src/NIAttributedLabel.m +++ b/src/NIAttributedLabel.m @@ -117,7 +117,7 @@ @interface NIAttributedLabel() @property (nonatomic) CTFrameRef textFrame; // CFType, manually managed lifetime, see setter. -@property (assign) BOOL detectingLinks; // Atomic. +@property (atomic, strong) id runningLinkDetection; @property (nonatomic) BOOL linksHaveBeenDetected; @property (nonatomic, copy) NSArray* detectedlinkLocations; @property (nonatomic, strong) NSMutableArray* explicitLinkLocations; @@ -272,6 +272,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText { // Clear the link caches. self.detectedlinkLocations = nil; + self.runningLinkDetection = nil; self.linksHaveBeenDetected = NO; [self removeAllExplicitLinks]; @@ -532,9 +533,9 @@ - (void)setHighlightedTextColor:(UIColor *)highlightedTextColor { } } -- (NSArray *)_matchesFromAttributedString:(NSString *)string { +- (NSArray *)_matchesFromAttributedString:(NSString *)string textCheckingTypes:(NSTextCheckingTypes)textCheckingTypes { NSError* error = nil; - NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)self.dataDetectorTypes + NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:textCheckingTypes error:&error]; NSRange range = NSMakeRange(0, string.length); @@ -542,15 +543,21 @@ - (NSArray *)_matchesFromAttributedString:(NSString *)string { } - (void)_deferLinkDetection { - if (!self.detectingLinks) { - self.detectingLinks = YES; - + if (self.runningLinkDetection == nil) { NSString* string = [self.mutableAttributedString.string copy]; + NSTextCheckingTypes textCheckingTypes = (NSTextCheckingTypes)self.dataDetectorTypes; + + self.runningLinkDetection = string; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSArray* matches = [self _matchesFromAttributedString:string]; - self.detectingLinks = NO; + NSArray* matches = [self _matchesFromAttributedString:string textCheckingTypes:textCheckingTypes]; dispatch_async(dispatch_get_main_queue(), ^{ + if (self.runningLinkDetection != string) { + // link detection was cancelled + return; + } + self.detectedlinkLocations = matches; self.linksHaveBeenDetected = YES; @@ -572,7 +579,7 @@ - (void)detectLinks { [self _deferLinkDetection]; } else { - self.detectedlinkLocations = [self _matchesFromAttributedString:self.mutableAttributedString.string]; + self.detectedlinkLocations = [self _matchesFromAttributedString:self.mutableAttributedString.string textCheckingTypes:(NSTextCheckingTypes)self.dataDetectorTypes]; self.linksHaveBeenDetected = YES; } } From 0d9077c0de5a2d51b4102725244f01d45d17047e Mon Sep 17 00:00:00 2001 From: Marc Knaup Date: Thu, 23 Oct 2014 14:53:18 +0200 Subject: [PATCH 2/2] Added fluidsonic as contributor to README.md. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e118545..a3f53f9 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,9 @@ NIAttributedLabel was extracted from Nimbus 1.2.0 by [Jeff Verkoeyen](http://jef Contributors ------------ -You can be the first! [Open a pull request now](https://github.com/NimbusKit/Basics/compare/). +- [fluidsonic](https://github.com/fluidsonic) + +Add yourself when contributing! [Open a pull request now](https://github.com/NimbusKit/Basics/compare/). License =======