2

当我的应用程序从背景启动时,我正在运行一个新线程以使用通知中心获取 AddresssBook 数据。这是我的代码,它显示了我如何调用该方法

    -(void)appLaunchedFromBackground:(NSNotification *) notification  {

    // NSThread *backgroundThread; is my ivar
    backgroundThread = [[NSThread alloc]initWithTarget:self selector:@selector(getUpdatedAddressBookData) object:nil];
    [backgroundThread start];
}

-(void)getUpdatedAddressBookData {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    AddressBook *addBook = [[AddressBook alloc]init];
    [addBook fetchAddressBookDataInBackground];
    [addBook release];
    [pool drain];
}

这是我的 fetchAddressBookDataInBackground 方法的代码

    -(void)fetchAddressBookDataInBackground {

    if (self.tempArray == nil) {
        NSMutableArray *temp = [[NSMutableArray alloc]init];
        self.tempArray = temp;
        [temp release];
    }

    ABAddressBookRef addressBook = ABAddressBookCreate();

    NSArray *tempPeople = [[NSArray alloc]init];

    tempPeople = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
    APP_DELGATE.people = [NSArray arrayWithArray:tempPeople];


    int peoCount = [APP_DELGATE.people count];

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

        ABRecordRef record = [APP_DELGATE.people objectAtIndex:i];

        NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(record)];


        // 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 *tempPhNos = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);

        NSArray* phoneNumbers = [self getPhoneNoWithoutSymbols:tempPhNos];

        NSString *strPhoneNos = [self getStringRepresentaionFromArray:phoneNumbers];

        // Get emails
        ABMultiValueRef emailProperty = ABRecordCopyValue(record, kABPersonEmailProperty);
        NSArray* emails = (NSArray*)ABMultiValueCopyArrayOfAllValues(emailProperty);
        NSString *strEmails = [self getStringRepresentaionFromArray:emails];


        // Get URL
        ABMultiValueRef urlProperty = ABRecordCopyValue(record, kABPersonURLProperty);
        NSArray* urls = (NSArray*)ABMultiValueCopyArrayOfAllValues(urlProperty);
        NSString *strURLs = [self getStringRepresentaionFromArray:urls];


        // Get Address
        ABMultiValueRef address=ABRecordCopyValue(record, kABPersonAddressProperty);
        CFDictionaryRef dic=nil;
        NSMutableArray *addressArray = [[NSMutableArray alloc]init];
        for (int index=0; index<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];

            }
            [labelName release];
            CFRelease(dic);
        }

        NSString *strAddress = [self getStringRepresentaionFromArray:addressArray];


        // 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];
        }

        NSString *fullName = [NSString stringWithFormat:@"%@ %@",fnm,lnm];

        // Create User object & add it to array

        User *user = [[User alloc]initUserWithUniqID:recordId.intValue FirstName:fnm lastName:lnm compositeName:fullName company:comp phoneNumbers:strPhoneNos emails:strEmails urls:strURLs address:strAddress notes:noteString dob:birthdateString userImage:image];

        [self.tempArray addObject:user];

        CFRelease(phoneNumberProperty);
        [tempPhNos release];
        CFRelease(emailProperty);
        [emails release];
        CFRelease(urlProperty);
        [urls release];
        CFRelease(address);
        [addressArray release];
        [birthDate release];
        [comp release];
        [noteString release];
        [lnm release];
        [fnm release];

        [user release];
    }

    [tempPeople release];
    CFRelease(addressBook);
    addressBook = nil;
    self.tempArray = [NSMutableArray arrayWithArray:[self.tempArray sortedArrayUsingSelector:@selector(compare:)]];

    APP_DELGATE.allUsersArray = self.tempArray;

    NSDictionary *dic = [NSDictionary dictionaryWithObject:self.tempArray forKey:BATCH_DONE_KEY];
    [[NSNotificationCenter defaultCenter] postNotificationName:BACKGROUND_WORK_DONE_NOTIFICATION object:self userInfo:dic];


}

-(NSMutableArray*)getPhoneNoWithoutSymbols:(NSArray*)array {

    if (self.phNoArray == nil) {
        NSMutableArray *temp = [[NSMutableArray alloc]init];
        self.phNoArray = temp;
        [temp release];
    }

    [self.phNoArray removeAllObjects];

    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];
}

但是我的应用程序在我使用“ABRecordCopyValue”函数的任何行都因“程序收到 EXC_BAD_ACCESS”错误而崩溃。我错过了什么?我的代码没有什么问题?

我尝试设置 NSZombieEnabled = YES ,但它没有显示任何消息。只需使用 ABRecordCopyValue 函数在任何行中说“程序收到 EXC_BAD_ACCESS”,在控制台中我看到(gdb)就是这样。

非常感谢任何帮助。谢谢。

4

0 回答 0