0

steps to cause problem -edit a field in CNContact, so let's say i'm editing the email of "email home". edit works successfully

- (void)doUpdateField {
    NSString * tempString = [[[self contentView] phoneField] text];

    [self.navigationItem setRightBarButtonItem:self.loadingView];
    [self.activityView startAnimating];

    CNMutableContact * contactToUpdate = [self.incomingContact mutableCopy];
    NSMutableArray * arrEmails = [self.incomingArray mutableCopy];
    [arrEmails removeObjectAtIndex:self.incomingIndexPath.row];
    CNLabeledValue * label = self.incomingArray[self.incomingIndexPath.row];

    CNLabeledValue * emailValue = [CNLabeledValue labeledValueWithLabel:label.label value:tempString];

    [arrEmails addObject:emailValue];
    [contactToUpdate setEmailAddresses:arrEmails];

    NSError *error;
    CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
    [saveRequest updateContact:contactToUpdate];

    BOOL success = [self.contactStore executeSaveRequest:saveRequest error:&error];
    if (success) {
        NSLog(@"success");
        [self.navigationItem setRightBarButtonItem:self.normalButton];
        [self.activityView stopAnimating];
    } else {
        NSLog(@"error %@", error);
        [self.navigationItem setRightBarButtonItem:self.normalButton];
        [self.activityView stopAnimating];
    }
}

-i then refresh the contact like so to repull the information

CNContact * refreshedContact = [self.contactStore unifiedContactWithIdentifier:self.incomingContact.identifier keysToFetch:keys error:&error];

-i then try to edit another field in the refreshedContact, but i get the following error:

Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNKeyPaths=(
    emailAddresses
)}

this makes absolutely no sense, i have no idea why i'm only able to edit the contact one time. i don't get this error if i never call the fucntion to refresh the contact. if i dont' refresh the contact, i can continue updating the CNContact like normal. any help would be great appreciated

Larry Pickles
  • 4,615
  • 2
  • 19
  • 36

1 Answers1

0

i just figured it out. Contacts framework is a mess. instead of calling

CNLabeledValue * emailValue = [CNLabeledValue labeledValueWithLabel:label.label value:tempString];

you must call this

CNLabeledValue * emailValue = [label labeledValueBySettingValue:tempString];

now it works. the reason it works is because it's not assigned a new id. see the apple docs.

Larry Pickles
  • 4,615
  • 2
  • 19
  • 36