Note that there are some explanatory texts on larger screens.

plurals
  1. POReduce time to perform big operation on iphone app
    text
    copied!<p>When I launch an app, I need to retrieve all contacts from Address Book &amp; store it in array to display it in table view. I wrote this code in viewDidAppear method. While contacts are retrieving from Address Book , I am showing activity indicator. I have around 1100 contacts in my address book. For this it <strong>took 14 seconds to retrieve data &amp; store it in array</strong>. Its not acceptable time. So I need to optimize this &amp; reduce time to max 2 to 3 seconds. </p> <p>I need all contacts because when my app launches , I need to search for contacts so I need all the data available in my array. </p> <p><strong>How can I reduce this timing</strong> ? If you need more information just let me know. Any kind of help is highly appreciated. Thanks.</p> <p>UPDATE 1 : My code </p> <pre><code> - (NSMutableArray*)getAddressBookData { self.tempArray = [[NSMutableArray alloc]init]; addressBook = ABAddressBookCreate(); APP_DELGATE.people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); peopleCount = [APP_DELGATE.people count]; for (int i=0; i&lt;peopleCount; i++) { ABRecordRef record = [APP_DELGATE.people objectAtIndex:i]; NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(record)]; NSLog(@"record id is %@",recordId); // Get fname, lname, company NSString *fnm = (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty) ; NSString *lnm = (NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty) ; NSString *comp = (NSString*)ABRecordCopyValue(record,kABPersonOrganizationProperty); // Get Ph no ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(record, kABPersonPhoneProperty); NSArray* phoneNumbers = [self getPhoneNoWithoutSymbols:(NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty)]; NSString *strPhoneNos = [self getStringRepresentaionFromArray:phoneNumbers]; NSLog(@"strPhoneNos =&gt; %@",strPhoneNos); // Get emails ABMultiValueRef emailProperty = ABRecordCopyValue(record, kABPersonEmailProperty); NSArray* emails = (NSArray*)ABMultiValueCopyArrayOfAllValues(emailProperty); NSString *strEmails = [self getStringRepresentaionFromArray:emails]; NSLog(@"strEmails =&gt; %@",strEmails); // Get URL ABMultiValueRef urlProperty = ABRecordCopyValue(record, kABPersonURLProperty); NSArray* urls = (NSArray*)ABMultiValueCopyArrayOfAllValues(urlProperty); NSString *strURLs = [self getStringRepresentaionFromArray:urls]; NSLog(@"strURLs =&gt; %@",strURLs); // Get Address ABMultiValueRef address=ABRecordCopyValue(record, kABPersonAddressProperty); CFDictionaryRef dic=nil; NSMutableArray *addressArray = [[NSMutableArray alloc]init]; for (int index=0; index&lt;ABMultiValueGetCount(address); index++) { dic=ABMultiValueCopyValueAtIndex(address, index); NSString* labelName=(NSString*)ABMultiValueCopyLabelAtIndex(address, index); if (labelName) { NSString *street =(NSString*) CFDictionaryGetValue(dic, kABPersonAddressStreetKey); NSString *city= (NSString*)CFDictionaryGetValue(dic, kABPersonAddressCityKey) ; NSString *state= CFDictionaryGetValue(dic, kABPersonAddressStateKey); NSString *country=CFDictionaryGetValue(dic, kABPersonAddressCountryKey); NSString *zipcode=CFDictionaryGetValue(dic, kABPersonAddressZIPKey); NSString *addressDetails=@""; if (street) { addressDetails=[NSString stringWithFormat:@"%@ ",street]; } if (city) { addressDetails=[NSString stringWithFormat:@"%@ %@ ",addressDetails,city]; } if (state) { addressDetails=[NSString stringWithFormat:@"%@ %@ ",addressDetails,state]; } if (country) { addressDetails=[NSString stringWithFormat:@"%@ %@ ",addressDetails,country]; } if (zipcode) { addressDetails=[NSString stringWithFormat:@"%@ %@ ",addressDetails,zipcode]; } [addressArray addObject:addressDetails]; } } NSString *strAddress = [self getStringRepresentaionFromArray:addressArray]; NSLog(@"strAddress =&gt; %@",strAddress); // Get Notes NSString *noteString=(NSString *)ABRecordCopyValue(record, kABPersonNoteProperty); // Get Birthdate NSDate *birthDate=(NSDate*)ABRecordCopyValue(record, kABPersonBirthdayProperty) ; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MMMM dd yyyy"]; NSString *birthdateString = [formatter stringFromDate:birthDate]; [formatter release]; // Get user image UIImage *image = nil; if( ABPersonHasImageData( record ) ) { NSData *imageData = (NSData*)ABPersonCopyImageData(record); image = [UIImage imageWithData:imageData]; [imageData release]; } // Create User object &amp; add it to array User *user = [[User alloc]initUserWithUniqID:recordId.intValue FirstName:fnm lastName:lnm company:comp phoneNumbers:strPhoneNos emails:strEmails urls:strURLs address:strAddress notes:noteString dob:birthdateString userImage:image]; [self.tempArray addObject:user]; [user release]; } self.tempArray = [NSMutableArray arrayWithArray:[self.tempArray sortedArrayUsingSelector:@selector(compare:)]]; APP_DELGATE.allUsersArray = self.tempArray; return tempArray; } -(NSMutableArray*)getPhoneNoWithoutSymbols:(NSArray*)array { self.phNoArray = [[NSMutableArray alloc]init]; for (NSString *str in array) { [self.phNoArray addObject:[self getPhNo:str]]; } return self.phNoArray; } -(NSString*)getPhNo:(NSString*)str { NSString *str0 = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; NSString *str1 = [str0 stringByReplacingOccurrencesOfString:@"(" withString:@""]; NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@")" withString:@""]; NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"-" withString:@""]; return str3; } -(NSString*)getStringRepresentaionFromArray:(NSArray*)array { return [array componentsJoinedByString:DELIMITER_SYMBOL]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload