2015-10-24 3 views
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 
    ) 

, а также глубокий подход ошибки с помощью Google является

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 

Ho w Я бы знал, что блок завершен или просто обработчик компиляции?

Если я вызываю метод, который в последней строке блока вызывает вызовы до завершения блока.

Любая помощь в руководстве?

+0

Вы загрузите таблицу из 'finalUsage' и вы вызываете этот метод из обработчика завершения на 'enumerateContactsWithFetchRequest', который предположительно выполняется в фоновом режиме. – Paulw11

+0

Так что же такое решение? –

+0

Отправка перезагрузки таблицы в главной очереди – Paulw11

ответ

0

попробуйте это. это работа для меня. использовать для перезагрузки tableview внутри основной очереди.

dispatch_async(dispatch_get_main_queue(), ^{ 
[_tableview_contacts reloadData]; 
}); 
1

Если попытаться Символическое Breakpoint обнаружить проблему: -

enter image description here

Затем поместите ваш UI код Обновить в главном потоке

DispatchQueue.main.async {}