Question

I am adding a contact to the address book the standard way and have been for months, since updating my app from iOS6 to iOS7 I have noticed that when adding a new contact to the address book there is a delay after I think with updating the UI.

I use a popup library (LPPopup) and before this worked perfectly and still does in all other controllers.

But now when I am running my ABAddressBookSave

NSLog(@"Making Person");
ABRecordRef newPerson = ABPersonCreate();
                ABRecordSetValue(newPerson, kABPersonFirstNameProperty,(__bridge CFTypeRef)contact.fname, &error);
                ABRecordSetValue(newPerson, kABPersonLastNameProperty, (__bridge CFTypeRef)contact.lname, &error);
                ABRecordSetValue(newPerson, kABPersonJobTitleProperty,(__bridge CFTypeRef)contact.post, &error);
                ABRecordSetValue(newPerson, kABPersonNoteProperty, (__bridge CFTypeRef)contact.notes, &error);
                NSLog(@"Making Number");

                //Add my phone number
                ABMutableMultiValueRef PhoneVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
                ABMultiValueAddValueAndLabel(PhoneVar, (__bridge CFTypeRef)contact.landLine, kABPersonPhoneMainLabel, NULL);
                ABMultiValueAddValueAndLabel(PhoneVar, (__bridge CFTypeRef)contact.mobile, kABPersonPhoneMobileLabel, NULL);
                ABRecordSetValue(newPerson, kABPersonPhoneProperty, PhoneVar,nil);
                CFRelease(PhoneVar);


                if(imageView.image){
                    NSLog(@"Saving contact image");
                    NSData *dataRef = UIImagePNGRepresentation(imageView.image);
                    ABPersonSetImageData(newPerson, (__bridge CFDataRef)dataRef, nil);
                }
                NSLog(@"Making EMAIL");
                //Add my email address
                ABMutableMultiValueRef EmailVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
                ABMultiValueAddValueAndLabel(EmailVar,(__bridge CFTypeRef)contact.email,kABWorkLabel, NULL);
                ABRecordSetValue(newPerson, kABPersonEmailProperty, EmailVar,nil);
                CFRelease(EmailVar);
                NSLog(@"Making Address");

                ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
                NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
                [addressDictionary setObject:contact.address1 forKey:(NSString *) kABPersonAddressStreetKey];
                [addressDictionary setObject:contact.address2 forKey:(NSString *)kABPersonAddressCityKey];
                [addressDictionary setObject:contact.address3 forKey:(NSString *)kABPersonAddressStateKey];
                [addressDictionary setObject:contact.zip forKey:(NSString *)kABPersonAddressZIPKey];
                ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFTypeRef)(addressDictionary), kABWorkLabel, NULL);
                ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
                CFRelease(multiAddress);

                //Finally saving the contact in the address book
                ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
                CFRelease(newPerson);
                ABAddressBookSave(iPhoneAddressBook, &error);
                if (error != NULL){
                    NSLog(@"Saving contact failed.");
                    NSLog(@"ERROR - \n%@",error);
                } else {
                    NSLog(@"Contact Saved Successfully");
                    [self successfulSave];
                    CFRelease(iPhoneAddressBook);
                }

And Popup

    -(void)successfulSave{
    NSLog(@"POPUP");
    [[LPPopup appearance] setPopupColor:[UIColor blackColor]];
    LPPopup *popup = [LPPopup popupWithText:@"Contact Saved"];
    [popup setTextColor:[UIColor whiteColor]];

    [popup showInView:self.superview
        centerAtPoint:self.center
             duration:1
           completion:^{NSLog(@"Popup Done after");}];
}
Was it helpful?

Solution

You should make sure that your UI calls are made on the main queue, using either

dispatch_async(dispatch_get_main_queue(), ^{
    [self successfulSave];
}

Or you can use

[self performSelectorOnMainThread: @selector(successfulSave)];

I go into some detail on this in my tutorial: http://www.raywenderlich.com/63885/address-book-tutorial-in-ios

Look toward the end. I believe it is the last thing I cover.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top