0

我正在使用 Apple 的新框架加载联系人。

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

        }

    }];
 }

最终使用这个数组并加载到 TableView 上的第二种方法是......

-(void) finalUsage: (NSMutableArray*)_contactsArray {

items = [[NSMutableArray alloc] init];

for (CNContact *contact in _contactsArray) {

    _contact = [[Contacts alloc] init];

    _contact.contactNumber = [[NSMutableArray alloc] init];
    _contact.email = [[NSMutableArray alloc] init];


    NSLog(@"I am Given Name is %@", contact.givenName);
    NSLog(@"I am Given Name is %@", contact.familyName);


    _contact.fName = contact.givenName;
    _contact.lName = contact.familyName;


    for (CNLabeledValue * emailValue in contact.emailAddresses){
        NSString *emailString = emailValue.value;
        NSString *emailLabel = emailValue.label;

        NSLog(@"Contact is %@, %@", emailString, emailLabel);
        [_contact.email addObject:emailString];

    }

    NSLog(@"\n\n\n...");

    for (CNLabeledValue<CNPhoneNumber*> * phoneValue in contact.phoneNumbers){

        NSString *phoneString =phoneValue.value.stringValue;
        NSString *phoneLabel = phoneValue.label;

        NSLog(@"Contact is phone %@  %@", phoneString,phoneLabel);

        [_contact.contactNumber addObject:phoneString];

    }

    NSLog(@"\n\n\n");

    [items addObject:_contact];

    NSLog(@"no. of items in array %li",(unsigned long)items .count);


}

[self.tableData removeAllObjects];

 _tableData = items;

[self updateSectionTiles];

 _tableData = [self partitionObjects:_tableData collationStringSelector:@selector(fName)];

[_tableview_contacts setDelegate:self];
[_tableview_contacts setDataSource:self];

 [_tableview_contacts reloadData];

}

现在它给出了错误

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.


Stack:(
    0   CoreFoundation                      0x0000000183288f60 <redacted> + 148
    1   libobjc.A.dylib                     0x0000000197e3bf80 objc_exception_throw + 56
    2   CoreFoundation                      0x0000000183288e90 <redacted> + 0
    3   Foundation                          0x00000001842a72d8 <redacted> + 88
    4   Foundation                          0x00000001841294fc <redacted> + 36
    5   UIKit                               0x00000001889238c4 <redacted> + 64
    6   UIKit                               0x0000000188923b08 <redacted> + 548
    7   UIKit                               0x00000001889238d4 <redacted> + 80
    8   UIKit                               0x0000000188816a78 <redacted> + 240
    9   UIKit                               0x000000018881632c <redacted> + 116

    10  UIKit                               0x00000001888161b8 <redacted> + 504
    11  UIKit                               0x0000000188823b0c <redacted> + 1792
    12  UIKit                               0x0000000188b580e4 <redacted> + 88
    13  UIKit                               0x0000000188884e68 <redacted> + 364
    14  UIKit                               0x00000001888f8414 <redacted> + 304
    15  UIKit                               0x00000001888f7fd0 <redacted> + 296
    16  UIKit                               0x000000018890429c <redacted> + 720
    17  UIKit                               0x0000000188903e74 <redacted> + 212
    18  UIKit                               0x000000018890387c <redacted> + 1920
    19  ProjectDemo                           0x000000010014c61c -[InviteViewController finalUsage:] + 2900
    20  ProjectDemo                           0x000000010014b81c __61-[InviteViewController getAllContactsWithNewFrameworkOfApple]_block_invoke + 668
    21  Contacts                            0x0000000182cbe700 <redacted> + 72
    22  libdispatch.dylib                   0x0000000100bedca8 _dispatch_call_block_and_release + 24
    23  libdispatch.dylib                   0x0000000100bedc68 _dispatch_client_callout + 16
    24  libdispatch.dylib                   0x0000000100bfcec8 _dispatch_root_queue_drain + 2344
    25  libdispatch.dylib                   0x0000000100bfc590 _dispatch_worker_thread3 + 132
    26  libsystem_pthread.dylib             0x000000019886d470 _pthread_wqthread + 1092
    27  libsystem_pthread.dylib             0x000000019886d020 start_wqthread + 4
    )

并且使用谷歌的深度错误方法是

2015-10-24 13:15:53.079 ProjectDemo[1653:290082] WARNING: GoogleAnalytics 3.10 void GAIUncaughtExceptionHandler(NSException *) (GAIUncaughtExceptionHandler.m:49): Uncaught exception: *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

我怎么知道该块是结束,还是简单的编译处理程序?

如果我调用块的最后一行中的方法,它会在块结束之前调用。

任何指导帮助?

4

2 回答 2

1

应该尝试符号断点来检测问题:-

在此处输入图像描述

然后将您的 UI 更新代码放在主线程中

DispatchQueue.main.async {}
于 2017-07-20T10:58:25.047 回答
0

尝试这个。这对我有用。用于在主队列中重新加载 tableview。

dispatch_async(dispatch_get_main_queue(), ^{
 [_tableview_contacts reloadData];
});
于 2015-10-26T10:49:08.570 回答