Alterplay / APAddressBook

Easy access to iOS address book
MIT License
1.38k stars 192 forks source link

APAddressBook excessive memory consumption with a large number of contacts #163

Open jync opened 7 years ago

jync commented 7 years ago

We're using APAddressBook to load native contacts to search against. However, for some of our users who have > 10k native contacts, the app seems to be consuming too much memory, causing an unexpected JetSam event and terminating the app.

Looking at the method where contacts are being extracted:

- (NSArray *)allContactsWithContactFieldMask:(APContactField)fieldMask
{
    NSMutableArray *contacts = [[NSMutableArray alloc] init];
    if (!self.wrapper.error)
    {
        CFArrayRef peopleArrayRef = ABAddressBookCopyArrayOfAllPeople(self.wrapper.ref);
        CFIndex count = CFArrayGetCount(peopleArrayRef);
        for (CFIndex i = 0; i < count; i++)
        {
            ABRecordRef recordRef = CFArrayGetValueAtIndex(peopleArrayRef, i);
            APContact *contact = [self.builder contactWithRecordRef:recordRef fieldMask:fieldMask];
            [contacts addObject:contact];
        }
        CFRelease(peopleArrayRef);
    }
    return contacts.count > 0 ? contacts.copy : nil;
}

Would it be better if we call this method using an autorelease pool and batching up contacts? Is there a better way to handle the large volume of contacts with APAddressBook?

belkevich commented 6 years ago

Hi @jync,

Thank you for issue and sorry for delay. Unfortunately there is no another way to handle contacts except loadContacts method. But if you know proper solution or something that can help avoid memory warnings (like placing objects in autorelease pool) I'll add it to source code and release it