Skip to content

Commit e6c13fe

Browse files
committed
fix: prevent duplicating contacts across containers
1 parent c9e7c3d commit e6c13fe

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

contacts.mm

100755100644
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,17 @@ CNAuthorizationStatus AuthStatus() {
325325
}
326326

327327
// Returns all contacts in the CNContactStore matching an identifier
328-
NSArray *FindContactsWithIdentifier(const std::string &identifier_string, Napi::Array extra_keys) {
328+
NSArray *FindContactsWithIdentifier(const std::string &identifier_string,
329+
Napi::Array extra_keys) {
329330
CNContactStore *addressBook = [[CNContactStore alloc] init];
330331

331-
NSString *identifier = [NSString stringWithUTF8String:identifier_string.c_str()];
332+
NSString *identifier =
333+
[NSString stringWithUTF8String:identifier_string.c_str()];
332334

333-
NSArray *identifiers = @[identifier];
335+
NSArray *identifiers = @[ identifier ];
334336

335-
NSPredicate *predicate = [CNContact predicateForContactsWithIdentifiers:identifiers];
337+
NSPredicate *predicate =
338+
[CNContact predicateForContactsWithIdentifiers:identifiers];
336339

337340
return
338341
[addressBook unifiedContactsMatchingPredicate:predicate
@@ -412,9 +415,11 @@ CNAuthorizationStatus AuthStatus() {
412415

413416
Napi::Array contacts = Napi::Array::New(env);
414417
CNContactStore *addressBook = [[CNContactStore alloc] init];
415-
NSMutableArray *cncontacts = [[NSMutableArray alloc] init];
416418
Napi::Array extra_keys = info[0].As<Napi::Array>();
417419

420+
// This is a set so that contacts in multiple containers aren't duplicated.
421+
NSMutableSet *unordered_contacts = [[NSMutableSet alloc] init];
422+
418423
if (AuthStatus() != CNAuthorizationStatusAuthorized)
419424
return contacts;
420425

@@ -430,9 +435,10 @@ CNAuthorizationStatus AuthStatus() {
430435
keysToFetch:GetContactKeys(extra_keys)
431436
error:nil];
432437

433-
[cncontacts addObjectsFromArray:container_contacts];
438+
[unordered_contacts addObjectsFromArray:container_contacts];
434439
}
435440

441+
NSArray *cncontacts = [unordered_contacts allObjects];
436442
int num_contacts = [cncontacts count];
437443
for (int i = 0; i < num_contacts; i++) {
438444
CNContact *cncontact = [cncontacts objectAtIndex:i];

0 commit comments

Comments
 (0)