Skip to content

Commit

Permalink
Changed address book loading queue from main queue to local queue
Browse files Browse the repository at this point in the history
  • Loading branch information
belkevich committed Jun 12, 2014
1 parent ba743af commit a38b13d
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions Classes/APAddressBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@interface APAddressBook ()
@property (nonatomic, readonly) ABAddressBookRef addressBook;
@property (nonatomic, readonly) dispatch_queue_t localQueue;
@end

@implementation APAddressBook
Expand All @@ -30,19 +31,24 @@ - (id)init
NSLog(@"%@", (__bridge_transfer NSString *)CFErrorCopyFailureReason(*error));
return nil;
}

NSString *name = [NSString stringWithFormat:@"com.alterplay.addressbook.%ld",
(long)self.hash];
_localQueue = dispatch_queue_create([name cStringUsingEncoding:NSUTF8StringEncoding], NULL);
self.fieldsMask = APContactFieldDefault;
}
return self;
}

- (void)dealloc
{
self.addressBookExternalChangeCallback = nil;
if (_addressBook)
{
CFRelease(_addressBook);
}
self.addressBookExternalChangeCallback = nil;
#if !OS_OBJECT_USE_OBJC
dispatch_release(_localQueue);
#endif
}

#pragma mark - public
Expand Down Expand Up @@ -78,39 +84,41 @@ - (void)loadContactsOnQueue:(dispatch_queue_t)queue

ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef errorRef)
{
/* Since we're referencing the address book we requested access for in
* the completion handler we need to dispatch back to the main thread
* where we requested the access. The handler is called on an arbitrary
* queue. */
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(self.localQueue, ^
{
NSArray *array = nil;
NSError *error = nil;
if (granted) {
CFArrayRef peopleArrayRef = ABAddressBookCopyArrayOfAllPeople(self.addressBook);
NSUInteger contactCount = (NSUInteger)CFArrayGetCount(peopleArrayRef);
NSMutableArray *contacts = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < contactCount; i++) {
ABRecordRef recordRef = CFArrayGetValueAtIndex(peopleArrayRef, i);
APContact *contact = [[APContact alloc] initWithRecordRef:recordRef
fieldMask:fieldMask];
if (!filterBlock || filterBlock(contact)) {
[contacts addObject:contact];
}
}
[contacts sortUsingDescriptors:descriptors];
array = contacts.copy;
CFRelease(peopleArrayRef);
}
else if (errorRef) {
error = (__bridge NSError *)errorRef;
}

dispatch_async(queue, ^
{
if (completionBlock) {
completionBlock(array, error);
}
});
if (granted)
{
CFArrayRef peopleArrayRef = ABAddressBookCopyArrayOfAllPeople(self.addressBook);
NSUInteger contactCount = (NSUInteger)CFArrayGetCount(peopleArrayRef);
NSMutableArray *contacts = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < contactCount; i++)
{
ABRecordRef recordRef = CFArrayGetValueAtIndex(peopleArrayRef, i);
APContact
*contact = [[APContact alloc] initWithRecordRef:recordRef fieldMask:fieldMask];
if (!filterBlock || filterBlock(contact))
{
[contacts addObject:contact];
}
}
[contacts sortUsingDescriptors:descriptors];
array = contacts.copy;
CFRelease(peopleArrayRef);
}
else if (errorRef)
{
error = (__bridge NSError *)errorRef;
}

dispatch_async(queue, ^
{
if (completionBlock)
{
completionBlock(array, error);
}
});
});
});
}
Expand Down Expand Up @@ -140,7 +148,8 @@ - (void)setAddressBookExternalChangeCallback:(void (^)(NSDictionary *changes))ad
(__bridge void *)(self));

//Should unregister
} else if (addressBookExternalChangeCallback == nil)
}
else if (addressBookExternalChangeCallback == nil)
{
ABAddressBookUnregisterExternalChangeCallback(self.addressBook,
APAddressBookExternalChangeCallback,
Expand Down

0 comments on commit a38b13d

Please sign in to comment.