Question

I'm trying to save firstName and lastname. NSLog(firstName); prints correct value from addressbook, but _dossier.firstName is empty. Image is saving correctly.

ABAddressBookRef addressBook = ABAddressBookCreate();

for (TKAddressBook *ab in contacts) {
    NSNumber *personID = [NSNumber numberWithInt:ab.recordID];
    ABRecordID abRecordID = (ABRecordID)[personID intValue];
    ABRecordRef abPerson = ABAddressBookGetPersonWithRecordID(addressBook, abRecordID);

    NSString* firstName = nil;
    NSString* lastName = nil;


    // Check person image
    UIImage *personImage = nil;
    if (abPerson != nil && ABPersonHasImageData(abPerson)) {

        firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
                                                          kABPersonFirstNameProperty);
        NSLog(firstName);
        lastName = (__bridge NSString*)ABRecordCopyValue(abPerson,
                                                         kABPersonLastNameProperty);

        CFDataRef contactThumbnailData = ABPersonCopyImageDataWithFormat(abPerson, kABPersonImageFormatThumbnail);
        personImage = [UIImage imageWithData:(__bridge NSData*)contactThumbnailData];
        CFRelease(contactThumbnailData);
        [_document.managedObjectContext performBlock:^() {
            Dossier *dossier = [NSEntityDescription insertNewObjectForEntityForName:@"Dossier"
                                                             inManagedObjectContext:_document.managedObjectContext];

            _dossier.firstName = firstName;
            _dossier.lastName = lastName;

            dossier.photo = personImage;
        }];

    }
}
Was it helpful?

Solution

Inside the performBlock you have assigned firstName and lastName to _dossier instead of the newly created dossier object.

OTHER TIPS

In this line:

firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
        NSLog(firstName);                                                              kABPersonFirstNameProperty);

Your NSLog interrupts the statement. Unless the formatting somehow messed up really bad.

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