0

I am writing an app for editing address book group, it is running a lot of changes like remove all contact from book, add all contacts, etc. The problem is that when the methods are running the app freezes UI, I understand that it happens because of running in the main thread but when I am trying to do it in background, it has problems like crashing and wrong changes in the group. Who can help me with resolving that issue?

I think running that in for a lot of times is blocking my UI:

ABAddressBookSave(_addressBookRef, &cfError);
Neeku
  • 3,646
  • 8
  • 33
  • 43

1 Answers1

0

First solution:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
                                         (unsigned long)NULL), ^(void) {
   <Write your method here>
});

Second solution:

- (void)syncPersonOutOfAddressBook
{
    [NSThread detachNewThreadSelector:@selector(syncContactsThread) toTarget:self withObject:nil];
}

-(void)syncContactsThread{
    if([self getAccessPermission]){
        ABAddressBookRef addressBook;
        CFErrorRef error = nil;
        addressBook = ABAddressBookCreateWithOptions(NULL,&error);
        [self AddressBookUpdated:addressBook]; //this is method is to get address from device
    }

}

For more information click here.

Community
  • 1
  • 1
Sandeep
  • 766
  • 5
  • 16