如何使用Apple™x27更快地获取iOS联系人;s联系人框架是否有长联系人列表?

如何使用Apple™x27更快地获取iOS联系人;s联系人框架是否有长联系人列表?,ios,cncontact,cncontactstore,Ios,Cncontact,Cncontactstore,我正在使用CNContacts在iOS设备中获取电话簿联系人 当我的手机中有少量联系人(比如50个)时,很容易获取联系人 但是,当我有很多联系人(比如500-700)时,它会挂起/等待很长时间,以便将这些联系人从iOS phonebook中提取到我的应用程序阵列中 以前我使用的是以前的Apple框架的快速库,但现在我使用的是最新的Apple框架 我获取联系人的代码是 #pragma mark - Get All Contacts.... -(void)getAllContactsWithNew

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

当我的手机中有少量联系人(比如50个)时,很容易获取联系人

但是,当我有很多联系人(比如500-700)时,它会挂起/等待很长时间,以便将这些联系人从iOS phonebook中提取到我的应用程序阵列中

以前我使用的是以前的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)线程上运行。从中可以看出,它提到:

用户可以授予或拒绝访问网络上的联系人数据 以每项申请为基础。通过调用请求访问联系人数据 requestAccessForEntityType:completionHandler:method。这不会 在请求用户权限时阻止应用程序。 只有在第一次请求访问时才会提示用户;任何 后续CNContactStore调用将使用现有权限。T他 在任意队列上调用完成处理程序。建议这样做 在此完成中使用CNContactStore实例方法 处理程序而不是UI主线程。在以下情况下,此方法是可选的: CNContactStore在后台线程中使用。如果此方法不可用 使用后,CNContactStore可能会在用户处于活动状态时阻止您的应用程序 请求访问权限

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

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

我认为您可以在子线程中获取联系人,但当重新加载表时,它应该在主线程中运行。它由于在子线程中重新加载表而崩溃。