gpt4 book ai didi

ios - ABRecordCopyValue 中的 EXC 错误访问

转载 作者:行者123 更新时间:2023-11-28 21:45:57 26 4
gpt4 key购买 nike

我在获取随机人变量的生日或电子邮件时遇到 EXC_BAD_ACCESS 问题。我尝试使用以下代码,但当我使用 dispatch_async 运行它时失败了。

ABAddressBook addressBook = ABAddressBookCreateWithOptions(NULL, error);

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
int maxThreads = 8;
int chunkSize = (int)(nPeople/maxThreads);

for (int loopNum = 0; loopNum < maxThreads; loopNum++)
{
int initCondition = chunkSize*loopNum;
int termCondition = (loopNum==(maxThreads-1)) ? (int)nPeople : chunkSize*(loopNum+1);

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=initCondition; i<termCondition; i++)
{
@try {
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)) ? (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)) : @"";
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)) ? (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)) :@"";

NSString *birthDate = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonBirthdayProperty));

ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray *emailAddresses = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);

ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

NSMutableArray *numbers = [NSMutableArray arrayWithCapacity:ABMultiValueGetCount(phoneNumbers)];
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++)
{
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
phoneNumber = [self removeSpecialChars:phoneNumber];
if ([phoneNumber length] >= 8)
{
NSString *mobileNumber = [self getPhoneNuber:phoneNumber];;
if ([mobileNumber length] > 7 && [mobileNumber length] < 11)
[numbers addObject:mobileNumber];
}
}
NSString *mobileNumbers = [numbers componentsJoinedByString:@","];
}
@catch (NSException *exception)
{
NSLog(@"%@", exception);
}
}
});
}

当我在没有 dispatch_async block 的情况下运行代码时,它运行没有错误。我想用 dispatch_async block 来并行扫描地址簿并检测本地数据库的任何更改。

最佳答案

documentation for ABAddressBookRef它声明这些对象只能在一个线程上创建和使用。您的代码似乎在原始线程上创建了 ABAddressBookAVRecordRefs,然后从 dispatch_async block 访问它们。我建议寻找另一种方法来执行此操作,即在后台线程上“及时”创建 ABAddressBookAVRecordRefs

Another question还有一些更相关的信息...

关于ios - ABRecordCopyValue 中的 EXC 错误访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com