contact picker perf for contact with many phone numbers

Only consider first n phone numbers for a contact.

Some users have a "Spam" contact created by external apps which have thousands
of known telemarkers/scammers phone numbers, this pathological case causes a
slowdown in the presentation of the compose picker.
This commit is contained in:
Michael Kirk 2019-03-07 17:05:54 -08:00
parent 348cd6c480
commit b36a0061e1

View file

@ -1,5 +1,5 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
#import "Contact.h"
@ -45,7 +45,16 @@ NS_ASSUME_NONNULL_BEGIN
NSMutableArray<NSString *> *phoneNumbers = [NSMutableArray new];
NSMutableDictionary<NSString *, NSString *> *phoneNumberNameMap = [NSMutableDictionary new];
for (CNLabeledValue *phoneNumberField in cnContact.phoneNumbers) {
const NSUInteger kMaxPhoneNumbersConsidered = 50;
NSArray<CNLabeledValue *> *consideredPhoneNumbers;
if (cnContact.phoneNumbers.count <= kMaxPhoneNumbersConsidered) {
consideredPhoneNumbers = cnContact.phoneNumbers;
} else {
OWSLogInfo(@"For perf, only considering the first %lu phone numbers for contact with many numbers.", (unsigned long)kMaxPhoneNumbersConsidered);
consideredPhoneNumbers = [cnContact.phoneNumbers subarrayWithRange:NSMakeRange(0, kMaxPhoneNumbersConsidered)];
}
for (CNLabeledValue *phoneNumberField in consideredPhoneNumbers) {
if ([phoneNumberField.value isKindOfClass:[CNPhoneNumber class]]) {
CNPhoneNumber *phoneNumber = (CNPhoneNumber *)phoneNumberField.value;
[phoneNumbers addObject:phoneNumber.stringValue];