Reputation: 39376
I'm having an issue with ABRecordRef's
stored in an array. This seems very similar to the issue in this stackoverflow. However, instead of just finding a workaround, I am looking to figure out what the issue is. I have a feeling that I am just missing something really simple here.
Originally my issue was with obtaining a thumbnail image for each Person
that had an image. I have since simplified the code in a test app and I have the same issue with ABPersonHasImageData
too.
Very simply, I get ABRecordRef's
and add them to an NSMutableArray
. After I have added them, I do a number of check's to see if they have image data. They all do initially. Later, almost all the entries in my NSMutableArray
will tell me that they have no image data, except for the first one in the array. I can get first names, last names, and many other values from those same ABRecordRefs
, but not the image data.
Here is my code.
I add the ABRecordRefs
in this manner:
- (void) storePerson:(ABRecordRef) person
{
if (!_persons) {
_persons = [[NSMutableArray alloc] init];
}
[_persons addObject:(__bridge id)(person)];
}
Right away, I run a check, and repeat that check every so often:
- (void) checkPersons
{
for (UInt16 i = 0; i < _persons.count; i++) {
ABRecordRef person = (__bridge ABRecordRef)(_persons[i]);
NSData *data = (__bridge_transfer NSData *)(ABPersonCopyImageDataWithFormat (person, kABPersonImageFormatThumbnail));
NSLog(@"index %d is %@ and it %@ image", i, person, ABPersonHasImageData(person)?@"HAS an": @"has NO");
}
}
The results for the immediate check and one a second later is:
2013-07-05 00:45:30.980 lmiy[5300:907] index 0 is <CPRecord: 0x1c59d8b0 ABPerson> and it HAS an image
2013-07-05 00:45:31.893 lmiy[5300:907] index 0 is <CPRecord: 0x1c59d8b0 ABPerson> and it HAS an image
Then I add another person to the array. The first person now doesn't seem to have an image anymore:
2013-07-05 00:45:40.080 lmiy[5300:907] index 0 is <CPRecord: 0x1c59d8b0 ABPerson> and it has NO image
2013-07-05 00:45:40.082 lmiy[5300:907] index 1 is <CPRecord: 0x1c59aa60 ABPerson> and it HAS an image
I add a third person, and the second person loses their image too:
2013-07-05 00:48:30.867 lmiy[5300:907] index 0 is <CPRecord: 0x1c59d8b0 ABPerson> and it has NO image
2013-07-05 00:48:30.869 lmiy[5300:907] index 1 is <CPRecord: 0x1c59aa60 ABPerson> and it has NO image
2013-07-05 00:48:30.872 lmiy[5300:907] index 2 is <CPRecord: 0x1c59d450 ABPerson> and it HAS an image
It seems as if the image data is stored in a volatile fashion and is flushed somehow. I'd rather not have to get and store all the image data initially. What am I doing that is causing this? Is it wrong to hold on to ABRecordRefs
and use them in this manner?
UPDATE: More often than not, the first entry into my _persons array - the one at index 0 - will be the one that will return an image and the rest of the entries will indicate that they do not have an image.
Upvotes: 1
Views: 724