Skip to content

Commit

Permalink
fix: ABAddressBookRef created on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlsCorrea committed Oct 23, 2020
1 parent 6af2a5c commit c90dcc5
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/ios/CDVContacts.m
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,13 @@ - (void)createAddressBook:(CDVAddressBookWorkerBlock)workerBlock
// TODO: this probably should be reworked - seems like the workerBlock can just create and release its own AddressBook,
// and also this important warning from (http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/BasicObjects.html):
// "Important: Instances of ABAddressBookRef cannot be used by multiple threads. Each thread must make its own instance."
ABAddressBookRef addressBook;

CFErrorRef error = nil;
// CFIndex status = ABAddressBookGetAuthorizationStatus();
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
// NSLog(@"addressBook access: %lu", status);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
ABAddressBookRef addressBook;
CFErrorRef error = nil;
// CFIndex status = ABAddressBookGetAuthorizationStatus();
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
// NSLog(@"addressBook access: %lu", status);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
// callback can occur in background, address book must be accessed on thread it was created on
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
Expand All @@ -608,6 +608,8 @@ - (void)createAddressBook:(CDVAddressBookWorkerBlock)workerBlock
}
});
});
});

}

@end

0 comments on commit c90dcc5

Please sign in to comment.