Skip to content

Fixed race condition in deferred link detection #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======
Expand Down
25 changes: 16 additions & 9 deletions src/NIAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ @interface NIAttributedLabel() <UIActionSheetDelegate>

@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;
Expand Down Expand Up @@ -272,6 +272,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText {

// Clear the link caches.
self.detectedlinkLocations = nil;
self.runningLinkDetection = nil;
self.linksHaveBeenDetected = NO;
[self removeAllExplicitLinks];

Expand Down Expand Up @@ -532,25 +533,31 @@ - (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);

return [linkDetector matchesInString:string options:0 range:range];
}

- (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;

Expand All @@ -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;
}
}
Expand Down