gpt4 book ai didi

ios - 如何使用具有长联系人列表的 Apple 联系人框架更快地获取 iOS 联系人?

转载 作者:行者123 更新时间:2023-12-01 19:20:14 27 4
gpt4 key购买 nike

我正在使用 CNContacts 获取 iOS 设备中的电话簿联系人。

当我的手机中有少量联系人(例如 50 个)时,可以轻松获取联系人。

但是,当我有很多联系人(比如 500-700)时,它会挂起/等待很长时间才能将这些联系人从 iOS 电话簿提取到我的应用程序的数组中。

之前我用过https://github.com/Alterplay/APAddressBook这是以前 Apple 框架的快速库,但现在我使用最新的 Apple 框架。

我获取联系人的代码是......

#pragma mark - Get All Contacts....

-(void)getAllContactsWithNewFrameworkOfApple {

NSMutableArray *_contactsArray = [[NSMutableArray alloc]init];

CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusDenied) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
// [self presentViewController:alert animated:TRUE completion:nil];
return;
}

CNContactStore *store = [[CNContactStore alloc] init];

[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

// make sure the user granted us access

if (!granted) {
dispatch_async(dispatch_get_main_queue(), ^{
// user didn't grant access;
// so, again, tell user here why app needs permissions in order to do it's job;
// this is dispatched to the main queue because this request could be running on background thread
});
return;
}

NSError *fetchError;
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey,CNContactGivenNameKey,CNContactFamilyNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey]];

BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {

[_contactsArray addObject:contact];
NSLog(@"I am %@", _contactsArray);

}];

if (!success) {
NSLog(@"error = %@", fetchError);
}else {

NSLog(@"End with Success %@", _contactsArray);
[self finalUsage:_contactsArray];

}

}];



}

我试图批量获取 20 个联系人,每次都重新加载表。但是这里它无法在调度线程中重新加载,或者只是崩溃。

如何改进此代码以快速获取联系人?

谢谢。

最佳答案

正如 Toro 提到的,requestAccessForEntityType:completionHandler: 方法不在主 (UI) 线程上运行。来自 documentation你可以看到它提到:

Users are able to grant or deny access to contact data on a per-application basis. Request access to contact data by calling requestAccessForEntityType:completionHandler: method. This will not block your application while the user is being asked for permission. The user will only be prompted the first time access is requested; any subsequent CNContactStore calls will use the existing permissions. The completion handler is called on an arbitrary queue. It is recommended that you use CNContactStore instance methods in this completion handler instead of the UI main thread. This method is optional when CNContactStore is used in the background thread. If this method is not used, CNContactStore may block your application while the user is asked for access permission.

因此,您想要在 UI 中执行的任何更新都必须在主线程上执行。

dispatch_async(dispatch_get_main_queue(), ^{
//Update UI on the Main thread, like reloading a UITableView
});

关于ios - 如何使用具有长联系人列表的 Apple 联系人框架更快地获取 iOS 联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33604817/

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