0

I have used following code block in order to retrieve the contacts. The problem is, non of the values are assigned it seems in "contacts" array as expected at the end. I'm very new to threading in iOS. If someone could help me in this, thanks a bunch.

CFErrorRef * error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
{
    if (granted)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
            CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

            for(int i = 0; i < numberOfPeople; i++){

                ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
                NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
                NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
                NSString *name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];


                ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
                NSUInteger j = 0;
                if (emails) {
                    for (j = 0; j < ABMultiValueGetCount(emails); j++) {
                        NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                        if (j == 0) {
                            [emailConatcts setValue:email forKey:name];
                            [contacts addObject:emailConatcts];
                        }
                    }
                }
                    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
                    if (phoneNumbers) {
                        CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
                        for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
                            CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
                            if (label) {
                                if (CFEqual(label, kABPersonPhoneMobileLabel)) {
                                    NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                                    [mobileContacts setValue:phoneNumber forKey:name];
                                    [contacts addObject:emailConatcts];
                                }
                            }
                        }
                    }
               }
        });
    }

});
NSLog(@"contacts: %@", contacts); 
Dhammini F.
  • 339
  • 3
  • 17
  • http://stackoverflow.com/questions/22754921/how-to-fetch-name-email-id-and-all-phone-numbers-from-addressbook-and-show-in-m/22755026#22755026 – Kirit Modi Apr 01 '14 at 11:47

1 Answers1

0

You are asking for the contacts before you have them apparently. If you move this line

NSLog(@"contacts: %@", contacts); 

before this

        });
    }
});

You should see the data.

The reason is, when you use dispach, and completion blocks, you are telling the system to run that code (usually in a different thread) after a certain operation has finished. So when you were printing the contacts, that block code had not run yet.

So whatever you need to do with the contacts, call it inside that block :)

Tiago Lira
  • 2,543
  • 1
  • 18
  • 17