Skip to content

Commit

Permalink
implements unified people, see heardrwt#33.
Browse files Browse the repository at this point in the history
unify duplicates records if the contact exists in multiple sources, see
http://stackoverflow.com/a/11480352/255463 and heardrwt#33.
  • Loading branch information
nebiros committed Apr 23, 2015
1 parent 535e4ad commit 98e5120
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RHAddressBook/RHAddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ typedef NS_ENUM(NSUInteger, RHAuthorizationStatus) {
@property (nonatomic, readonly, copy) NSArray *peopleOrderedByFirstName;
@property (nonatomic, readonly, copy) NSArray *peopleOrderedByLastName;

- (NSArray *)peopleUnifiedUsingDefaultSource;
- (NSArray *)peopleUnifiedUsingSource:(RHSource *)source;

-(NSArray*)peopleWithName:(NSString*)name;
-(NSArray*)peopleWithEmail:(NSString*)email;
-(RHPerson*)personForABRecordRef:(ABRecordRef)personRef; //returns nil if ref not found in the current ab, eg unsaved record from another ab. if the passed recordRef does not belong to the current addressbook, the returned person objects underlying personRef will differ from the passed in value. This is required in-order to maintain thread safety for the underlying AddressBook instance.
Expand Down
72 changes: 72 additions & 0 deletions RHAddressBook/RHAddressBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,45 @@ -(NSArray*)peopleOrderedByLastName{
return [self peopleOrderedBySortOrdering:kABPersonSortByLastName];
}

/**
*
*
* @return NSArray
*/
- (NSArray *)peopleUnifiedUsingDefaultSource
{
__block NSArray *result = nil;

rh_dispatch_sync_for_addressbook(self, ^{
result = arc_retain([self peopleUnifiedUsingSource:[self defaultSource]]);
});

return arc_autorelease(result);
}

/**
*
*
* @param source
*
* @return NSArray
*/
- (NSArray *)peopleUnifiedUsingSource:(RHSource *)source
{
__block NSArray *result = nil;

rh_dispatch_sync_for_addressbook(self, ^{
CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeopleInSource(_addressBookRef, source.recordRef);

if (peopleRefs) {
result = arc_retain([self peopleUnifiedForABRecordRefs:peopleRefs]);
CFRelease(peopleRefs);
}
});

return arc_autorelease(result);
}

-(NSArray*)peopleWithName:(NSString*)name{
__block NSArray *result = nil;
rh_dispatch_sync_for_addressbook(self, ^{
Expand Down Expand Up @@ -778,6 +817,39 @@ -(NSArray*)peopleForABRecordRefs:(CFArrayRef)peopleRefs{
return [NSArray arrayWithArray:people];
}

/**
*
*
* @param peopleRefs
* @see http://stackoverflow.com/a/11480352/255463
*
* @return NSArray
*/
- (NSArray *)peopleUnifiedForABRecordRefs:(CFArrayRef)peopleRefs
{
if (!peopleRefs) return nil;

NSMutableSet *people = [NSMutableSet set];

rh_dispatch_sync_for_addressbook(self, ^{
for (CFIndex i = 0; i < CFArrayGetCount(peopleRefs); i++) {
NSMutableSet *contactSet = [NSMutableSet set];
ABRecordRef personRef = CFArrayGetValueAtIndex(peopleRefs, i);
[contactSet addObject:(__bridge id) personRef];

NSArray *linkedRecordsArray = (__bridge NSArray *) ABPersonCopyArrayOfAllLinkedPeople(personRef);
[contactSet addObjectsFromArray:linkedRecordsArray];

RHPerson *person = [self personForABRecordRef:personRef];
if (person) [people addObject:person];

CFRelease(personRef);
}
});

return people.allObjects;
}

-(RHPerson*)personForABRecordID:(ABRecordID)personID{

__block ABRecordRef recordRef = NULL;
Expand Down

0 comments on commit 98e5120

Please sign in to comment.