-1

For some reason, I am getting a crash (noted below) saying "Collection <__NSCFString: 0x155cefd0> was mutated while being enumerated", but only on an actual device. It works on the Xcode test device. I'm not entirely sure what that means, so how do I approach this lldb crash?

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
        ABRecordRef pet = ABPersonCreate();
        ABRecordSetValue(pet, kABPersonFirstNameProperty, (__bridge CFStringRef)petFirstName, nil);
        ABRecordSetValue(pet, kABPersonLastNameProperty, (__bridge CFStringRef)petLastName, nil);

        ABMutableMultiValueRef phoneNumbers = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(phoneNumbers, (__bridge CFStringRef)petPhoneNumber, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(pet, kABPersonPhoneProperty, phoneNumbers, nil);

        ABAddressBookAddRecord(addressBookRef, pet, nil);

        NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
        for (id record in allContacts){
            ABRecordRef thisContact = (__bridge ABRecordRef)record; //*******The line below is where the crash is occurring*********   
            if (CFStringCompare(ABRecordCopyCompositeName(thisContact),
                                ABRecordCopyCompositeName(pet), 0) == kCFCompareEqualTo){
                //The contact already exists!
                //detects if duplicate

                return;
            }
        }
Larme
  • 24,190
  • 6
  • 51
  • 81
user3642059
  • 93
  • 1
  • 6

1 Answers1

0

You are changing a collection at some other point of your application. According to Apple docs "It is not safe to remove, replace, or add to a mutable collection’s elements while enumerating through it"
Please check the following:

  1. Which collections are you working with in your app, that could be modify while your above code is running.
  2. If you need to modify any collection while iterating through it, use suggested techniques in linked docs.
Barbara R
  • 1,057
  • 5
  • 18