ABAddressBook搜索逻辑,潜在的泄漏

时间:2013-10-16 13:42:42

标签: ios memory-leaks addressbook

我有这个代码用于在AddressBook中搜索,xcode建议我在firstName,lastName和另一个甚至xcode都未知的对象中有潜在的泄漏。我试图释放它们中的前两个并且警告消失但我的应用程序崩溃了。此外,我在这一行有一个随机的崩溃,我只得到它一次,但出现在其他人的崩溃。你看到我的代码有什么问题吗?感谢。

ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)evaluatedObject, kABPersonPhoneProperty);

代码。它的目的是按名称的第一个字母对所有联系人进行分组,并在有多个号码的情况下复制联系人。

- (void)filterByKeyword:(NSString*)keyword completionBlock:(AddressBookSearchCompletionBlock)completionBlock {

dispatch_async(abQueue, ^{

    [self.filteredContacts removeAllObjects];

    // Iterate over original contacts

    for (id evaluatedObject in self.allContacts) {

        CFStringRef n = ABRecordCopyCompositeName((__bridge ABRecordRef)evaluatedObject);
        NSString *name = (__bridge_transfer NSString*)n;

        if (name == nil) {
            RCLog(@"name is nil. skip this contact");
            continue;
        }
        if (keyword == nil || [[name lowercaseString] rangeOfString:keyword].location != NSNotFound)
        {
            NSString *key = [[name substringToIndex:1] uppercaseString];
            if (key == nil || [key isEqualToString:@"_"]) {
                key = @"#";
            }
            NSMutableArray *arr = [self.filteredContacts objectForKey:key];
            if (arr == nil) {
                arr = [[NSMutableArray alloc] init];
            }

            ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)evaluatedObject, kABPersonPhoneProperty);
            signed long num = ABMultiValueGetCount(phoneNumbers);

            if (num == 0) {
                [arr addObject:evaluatedObject];
            }
            else {
                for (CFIndex i = 0; i < num; i++) {

                    if (i > 0) {
                        // Create a separate contact with the alternative phone numbers of a contact
                        NSString* phoneNumber = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                        NSString* phoneNumericNumber = [phoneNumber stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
                        if (phoneNumber != nil && phoneNumber.length > 0 && phoneNumericNumber.length > 0) {

                            ABRecordRef persona = ABPersonCreate();

                            CFStringRef firstName = ABRecordCopyValue((__bridge ABRecordRef)evaluatedObject, kABPersonFirstNameProperty);
                            CFStringRef lastName = ABRecordCopyValue((__bridge ABRecordRef)evaluatedObject, kABPersonLastNameProperty);
                            ABRecordSetValue (persona, kABPersonFirstNameProperty, firstName, nil);
                            ABRecordSetValue (persona, kABPersonLastNameProperty, lastName, nil);

                            ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
                            bool didAddPhone = ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(phoneNumber), kABPersonPhoneMobileLabel, NULL);
                            if (didAddPhone){
                                ABRecordSetValue(persona, kABPersonPhoneProperty, multiPhone, nil);
                            }

                            if (ABPersonHasImageData((__bridge ABRecordRef)evaluatedObject)) {
                                ABPersonSetImageData(persona, ABPersonCopyImageDataWithFormat((__bridge ABRecordRef)evaluatedObject, kABPersonImageFormatThumbnail), nil);
                            }

                            [arr addObject:(__bridge id)persona];

                            CFRelease(multiPhone);
                            CFRelease(persona);
                        }
                    }
                    else {
                        [arr addObject:evaluatedObject];
                    }
                }
            }

            CFRelease(phoneNumbers);

            [self.filteredContacts setObject:arr forKey:key];
            RCLog(@"%i contacts for key %@", arr.count, key);
        }
    }

    self.filteredKeys = [[self.filteredContacts allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    dispatch_async(dispatch_get_main_queue(), ^{
        completionBlock();
    });
});

}

0 个答案:

没有答案