From a38b13debabc71ce6ec9836fbbbe61146c1d15a7 Mon Sep 17 00:00:00 2001 From: Alexey Belkevich Date: Thu, 12 Jun 2014 14:44:57 +0300 Subject: [PATCH] Changed address book loading queue from main queue to local queue --- Classes/APAddressBook.m | 77 +++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/Classes/APAddressBook.m b/Classes/APAddressBook.m index 5586bfd..a0cb209 100644 --- a/Classes/APAddressBook.m +++ b/Classes/APAddressBook.m @@ -12,6 +12,7 @@ @interface APAddressBook () @property (nonatomic, readonly) ABAddressBookRef addressBook; +@property (nonatomic, readonly) dispatch_queue_t localQueue; @end @implementation APAddressBook @@ -30,7 +31,9 @@ - (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; @@ -38,11 +41,14 @@ - (id)init - (void)dealloc { + self.addressBookExternalChangeCallback = nil; if (_addressBook) { CFRelease(_addressBook); } - self.addressBookExternalChangeCallback = nil; +#if !OS_OBJECT_USE_OBJC + dispatch_release(_localQueue); +#endif } #pragma mark - public @@ -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); + } + }); }); }); } @@ -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,