1

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;
        }];

    }
}
Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • 1
    What is `_dossier` ? Shouldn't it be `dossier.firstName = firstName; dossier.lastName = lastName;` ? – Martin R Sep 30 '12 at 10:44
  • _dossier is UIManagedDocument – Shmidt Sep 30 '12 at 14:15
  • 1
    I don't think this will help with the problem, but you should be using `__bridge_transfer` (or `CFBridgingRelease`) on the return values from `ABRecordCopyValue`. – Peter Hosey Sep 30 '12 at 14:25
  • What happens if you `NSLog(@"%@ %@", firstName, lastName)` within the block? – Peter Hosey Sep 30 '12 at 14:26
  • 1
    So you want to save `personImage` in the newly created `Dossier *dossier` object, but `firstName` and `lastName` in the `UIManagedDocument *_dossier` (which is a different object) ? – Martin R Sep 30 '12 at 14:33
  • Another point: `performBlock:` might execute *asynchronously* if the managed object context is of the private concurrency type. You can try if using `performBlockAndWait:` solves your problem. – Martin R Sep 30 '12 at 14:35
  • @MartinR Oh! Such a stupid mistake I made :) Thank you very much!!! – Shmidt Sep 30 '12 at 14:39
  • Which one was it? `dossier` vs. `_dossier`? I would like to add it as a formal answer, which you can "accept". – Martin R Sep 30 '12 at 14:40
  • Yes @MartinR , exactly. Please do that – Shmidt Sep 30 '12 at 14:42

2 Answers2

2

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

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

In this line:

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

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

Cezary Wojcik
  • 21,745
  • 6
  • 36
  • 36