Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

anonymous1
164 PointsHelp with looping through array in a loop
I am trying to get a list of numbers from the address book. I am then trying to get all users from parse.com that match the numbers found in the address book. In other words, I am trying to pull down all the users that someone would know / has stored in their address book ... using the mobile as the identifier.
I have it half working, but it appears to loop over and over until all checked and I end up with only 1 result in my view.
If anyone is able to help me re-factor how I am supposed to combine these things and alter my query or indeed offer best practice I would be most appreciative :)
////////////////////////////////////////////
// load up all the numbers for the user
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
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));
//NSLog(@"Name:%@ %@", firstName, lastName);
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSLog(@"the number%@", phoneNumber);
PFQuery *query = [PFUser query];
[query whereKey:@"mobile" equalTo:phoneNumber];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
self.allUsers = objects;
[self.tableView reloadData];
}
}];
self.currentUser =[PFUser currentUser];
}
}
}
1 Answer

Amit Bijlani
Treehouse Guest TeacherWhy not try the following:
- Build an array of phone numbers, let's call it
phoneNumbers
. - Download a list of all users from Parse, let's call it
users
. - Loop through all the
users
and create a new array calledfriends
. Only add objects to thefriends
if their phone number exists in thephoneNumbers
array using thecontainsObject
method.