我是iphone开发的新手。目前我正在开发一个应用程序,它将联系人从iphone联系人导入应用程序并将其存储到sqlite数据库。在iphone 5中导入大量联系人时,应用程序崩溃。您可以在下面找到用于插入的编码。可以告诉任何人有什么问题,或者如果有更好的方法,请提出建议。谢谢提前
CrashLog:
0 PList 0x0014ceb6 testflight_backtrace + 238
1 PList 0x0014db9c TFSignalHandler + 264
2 libsystem_c.dylib 0x3613dd3a _sigtramp + 42
3 Platinum List 0x001377dc -[ContactList importcontact] + 2924
4 Platinum List 0x001377dc -[ContactList importcontact] + 2924
5 Platinum List 0x001357cc -[ContactList displaylist] + 468
6 Foundation 0x3693aa40 <redacted> + 972
7 libsystem_c.dylib 0x361160e0 <redacted> + 308
8 libsystem_c.dylib 0x36115fa7 thread_start + 7
- (void) importcontact{
[appDelegate resetIdleTimer]; // reset idle timer
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
totalContact = ABAddressBookGetPersonCount( addressBook );
int count = 0 ;
int counter = 0;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Db) == SQLITE_OK)
{
@autoreleasepool{
for(int j=0;j<totalContact;j=j+250){
[appDelegate resetIdleTimer]; // reset idle timer
int hasError=0;
sqlite3_exec(Db, "BEGIN EXCLUSIVE TRANSACTION", 0, 0, 0);
const char *insert_stmt = "INSERT INTO 'DemoTable' ('record_id','addressbook','create_date','modify_date','profile_image','gender','status') VALUES (?,?,?,?,?,?,?)";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(Db, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
int limiti=j+250;
if(limiti>totalContact){
limiti = totalContact;
}
for(int i=j;i<limiti;i++){
//NSLog(@"%d %d",i,totalContact);
ABRecordRef person = CFArrayGetValueAtIndex( people, i );
//Get the contact´s record id
NSMutableDictionary *record =[[[NSMutableDictionary alloc] init] autorelease];
NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)]; // record ID
int check =[self CheckRecordExists:recordId];
if(check==0){
int checkname = 0;
int checkphone = 0;
[record setObject:recordId forKey:@"recordid"];
//Get the contact´s profile image
BOOL checkProfilePic;
NSData *profilePic = (NSData *)ABPersonCopyImageData(person);
if (profilePic != nil){
checkProfilePic = TRUE;
//UIImage *personImage = [UIImage imageWithData:profilePic]; //profile image
}else{
checkProfilePic = FALSE;
profilePic = UIImageJPEGRepresentation([UIImage imageNamed:@"no-photo.png"], 10);
}
//Get the contact´s First Name
NSMutableDictionary *basicinformation =[[[NSMutableDictionary alloc] init] autorelease];
NSString *firstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
//Get the contact´s Last Name
NSString *lastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) autorelease];
//Get the contact´s Last Name
NSString *organization = [(NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty) autorelease];
//Get the contact´s Creation Date
NSDate *creationdate = [(NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty) autorelease];
//Get the contact´s Modification Date
NSDate *modificationdate = [(NSDate *)ABRecordCopyValue(person,kABPersonModificationDateProperty) autorelease];
if(firstName.length!=0){
checkname = 1 ;
[basicinformation setObject:firstName forKey:@"First"];
}else{
[basicinformation setObject:@"" forKey:@"First"];
}
if(lastName.length!=0){
checkname = 1;
[basicinformation setObject:lastName forKey:@"Last"];
}else{
[basicinformation setObject:@"" forKey:@"Last"];
}
if(organization.length!=0){
checkname = 1 ;
[basicinformation setObject:organization forKey:@"Company"];
}else{
[basicinformation setObject:@"" forKey:@"Company"];
}
//Get the contact´s phone numbers
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty); //phone numbers
if ([firstName length]>0 || [lastName length]>0 || ABMultiValueGetCount(phoneNumbers) > 0) {
counter++;
}
NSMutableDictionary *phoneNumberDict =[[[NSMutableDictionary alloc] init] autorelease];
if (ABMultiValueGetCount(phoneNumbers) > 0) {
checkphone = 1;
for(CFIndex j = 0; j < ABMultiValueGetCount(phoneNumbers); j++)
{
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, j);
CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(locLabel);//remove mark up character
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumbers, j);
NSString *phoneNumber = (NSString *)phoneNumberRef;
NSString *phoneLabel = (NSString *)labeltype;
[phoneNumberDict setObject:phoneNumber forKey:phoneLabel];
CFRelease(phoneNumberRef);
CFRelease(labeltype);
}
} else {
}
CFRelease(phoneNumbers);
//Get the contact´s emails
ABMultiValueRef emails = (ABMultiValueRef)ABRecordCopyValue(person,kABPersonEmailProperty); //emails
NSMutableDictionary *emailDict =[[[NSMutableDictionary alloc] init] autorelease];
if (ABMultiValueGetCount(emails) > 0) {
for(CFIndex j = 0; j < ABMultiValueGetCount(emails); j++)
{
CFStringRef emailabel = ABMultiValueCopyLabelAtIndex(emails, j);
CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(emailabel);//remove mark up character
CFStringRef emailId = ABMultiValueCopyValueAtIndex(emails, j);
NSString *emailIdstring = (NSString *)emailId;
NSString *emailIdlable = (NSString *)labeltype;
[emailDict setObject:emailIdstring forKey:emailIdlable];
CFRelease(labeltype);
CFRelease(emailId);
CFRelease(emailabel);
}
}else{
}
CFRelease(emails);
//Get the contact´s addresses
ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty); //address
NSMutableDictionary *addressDict =[[[NSMutableDictionary alloc] init] autorelease];
if (ABMultiValueGetCount(addresses) > 0) {
for (CFIndex j = 0; j<ABMultiValueGetCount(addresses);j++){
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addresses, j);
CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(addresses, j);
CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(typeTmp);
NSString *addressLabel = (NSString *)labeltype;
NSString *street = [[(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy] autorelease];
NSString *city = [[(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy] autorelease];
NSString *state = [[(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy] autorelease];
NSString *zip = [[(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy] autorelease];
NSString *country = [[(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy] autorelease];
NSMutableDictionary *addressDictSplit =[[NSMutableDictionary alloc] init];
if(street.length!=0){
[addressDictSplit setObject:street forKey:@"street"];
}else{
[addressDictSplit setObject:@"" forKey:@"street"];
}
if(city.length!=0){
[addressDictSplit setObject:city forKey:@"city"];
}else{
[addressDictSplit setObject:@"" forKey:@"city"];
}
if(state.length!=0){
[addressDictSplit setObject:state forKey:@"state"];
}else{
[addressDictSplit setObject:@"" forKey:@"state"];
}
if(zip.length!=0){
[addressDictSplit setObject:zip forKey:@"zip"];
}else{
[addressDictSplit setObject:@"" forKey:@"zip"];
}
if(country.length!=0){
[addressDictSplit setObject:country forKey:@"country"];
}else{
[addressDictSplit setObject:@"" forKey:@"country"];
}
[addressDict setObject:addressDictSplit forKey:addressLabel];
[addressDictSplit release];
CFRelease(labeltype);
break;
}
}else{
}
CFRelease(addresses);
//Get the contact´s URL
ABMultiValueRef URL = ABRecordCopyValue(person, kABPersonURLProperty); //address
NSMutableDictionary *URLDict =[[[NSMutableDictionary alloc] init] autorelease];
if (ABMultiValueGetCount(URL) > 0) {
for (CFIndex j = 0; j<ABMultiValueGetCount(URL);j++){
CFStringRef URLlabel = ABMultiValueCopyLabelAtIndex(URL, j);
CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(URLlabel);//remove mark up character
CFStringRef URLstring = ABMultiValueCopyValueAtIndex(URL, j);
NSString *URLlabelString = (NSString *)labeltype;
NSString *sURL = (NSString *)URLstring;
[URLDict setObject:sURL forKey:URLlabelString];
CFRelease(URLstring);
CFRelease(labeltype);
}
}else{
}
CFRelease(URL);
NSMutableDictionary *userDict = [[NSMutableDictionary alloc] init];
[userDict setObject:record forKey:@"id"];
[userDict setObject:basicinformation forKey:@"basicinfo"];
[userDict setObject:phoneNumberDict forKey:@"phone"];
[userDict setObject:addressDict forKey:@"address"];
[userDict setObject:emailDict forKey:@"email"];
[userDict setObject:URLDict forKey:@"url"];
if(checkname==1 && checkphone==1){
NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:userDict
options:NSJSONWritingPrettyPrinted error:&error];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults stringForKey:@"new"];
NSString *temp = [NSString stringWithFormat:@"%d", [temp1 integerValue] + 1];
[defaults setObject:temp forKey:@"new"];
[defaults synchronize];
//storing profile pic to temporary folder
NSString *profileImageTempName=@"";
if(checkProfilePic==TRUE){
profileImageTempName = @"profileimage";
}else{
profileImageTempName = @"addphoto";
}
NSString *profileImagePath = [profileImageTempName stringByAppendingString:temp];
NSString *profilePath = [documentsDirectory stringByAppendingPathComponent:profileImagePath];
NSString *gender1 = @"Unknown";
int recorID = [recordId integerValue];
NSTimeInterval createDateTime = [creationdate timeIntervalSince1970];
NSTimeInterval modifiedTime = [modificationdate timeIntervalSince1970];
NSString *createDataeTimeString = [NSString stringWithFormat:@"%f", createDateTime];
NSString *modifiedTimeString = [NSString stringWithFormat:@"%f", modifiedTime];
sqlite3_bind_int(statement, 1, recorID);
sqlite3_bind_blob(statement, 2, [jsonData bytes], [jsonData length], NULL);
sqlite3_bind_text(statement, 3, [createDataeTimeString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [modifiedTimeString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [profilePath UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [gender1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 7, 1);
if (sqlite3_step(statement) != SQLITE_DONE) {
hasError=1;
NSLog(@"Prepare-error %s", sqlite3_errmsg(plaitnumDb));
}else{
count++;
NSString *changeString = [NSString stringWithFormat:@"%@ %d",@"Imported",count];
[self.label performSelectorOnMainThread:@selector(setText:) withObject:changeString waitUntilDone:NO];
[profilePic writeToFile:profilePath atomically:NO];
}
sqlite3_reset(statement);
}
[record release];
[basicinformation release];
[phoneNumberDict release];
[addressDict release];
[emailDict release];
[URLDict release];
}
}
sqlite3_finalize(statement);
if( hasError == 0 ) { // no error in inserting
[appDelegate resetIdleTimer]; // reset idle timer
if (sqlite3_exec(Db, "COMMIT TRANSACTION", 0, 0, 0) != SQLITE_OK) NSLog(@"SQL Error: %s",sqlite3_errmsg(Db));
// set the NSUserDefault to not import next time.
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
BOOL haveused = [standardUserDefaults boolForKey:@"alreadyImported"];
if(haveused)
{
//NOT FIRST TIME
}
else
{
// FIRST TIME
[standardUserDefaults setBool:TRUE forKey:@"alreadyImported"];
[standardUserDefaults synchronize];
}
}
else {
sqlite3_exec(Db,"ROLLBACK", 0, 0, 0);
}
}
}
}
sqlite3_close(Db);
}
}