1

I'm having trouble with ABPeoplePickerNavigationController generating zombies and freezing my app, I tried several methods to initialize it but someway or another seem to randomly freeze my app:

  if([appDelegate testInternetConnection]){

        ABPeoplePickerNavigationController *picker =[[ABPeoplePickerNavigationController alloc] init];
        [picker setPeoplePickerDelegate:self];
        [self presentViewController:picker animated:YES completion:nil ];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Internet Connection Error" message:@"This App needs internet in order to work, please make sure you are connected to a valid network" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            // Display/dismiss your alert
            [alert show];

        });


    }

I don't know what I'm doing wrong but this is freezing my app outsite the emulator or when the device it's not debugging. Any idea why?

-Update

Here is the code I'm using to save in Core Data

#pragma mark Save data to Core Data Safely
-(void) saveData{
    NSPersistentStoreCoordinator *mainThreadContextStoreCoordinator = [appDelegate persistentStoreCoordinator];
    dispatch_queue_t request_queue = dispatch_queue_create("com.4Postcards.savingData", NULL);
    dispatch_async(request_queue, ^{

        // Create a new managed object context
        // Set its persistent store coordinator
        NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];

        [newMoc setPersistentStoreCoordinator:mainThreadContextStoreCoordinator];

        // Register for context save changes notification
        NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
        [notify addObserver:self
                   selector:@selector(mergeChanges:)
                       name:NSManagedObjectContextDidSaveNotification
                     object:newMoc];

        // Do the work
        // Your method here
        // Call save on context (this will send a save notification and call the method below)
        NSError *error;
        BOOL success = [newMoc save:&error];
        if (!success)
            NSLog(@"%@",error);
        // Deal with error
    });
}

- (void)mergeChanges:(NSNotification*)notification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Data Saved");

    });
}

As I said now it doesn't freeze when running connected to Xcode, but when disconnected it does

ZurceZx
  • 11
  • 3

1 Answers1

0

It's due to core data. Remember, every time you initiate a change to data (e.g. update, delete, add objects). Make a new ManagedObjectContext with the same persistentStoreCoordinator. And add Didsave notification observer to the core data. What basically happens is core data is not thread safe. And let suppose your thread1 asks for data from entity1 and at the same time thread2 adding data to entity1 and thread3 deleting data from entry1. Execution will be stopped as concurrency is not being managed. For you is to study these links to have a better understanding and see the sample code also.

Apple Developer Concurrency with Core Data

Sample Code Apple Developer

StackoverFlow Detail

A blog with code examples

Community
  • 1
  • 1
Saad
  • 8,857
  • 2
  • 41
  • 51
  • Hi! Great response, but still the same issue, I changed the way I saved the data in Core Data and now it freeze at the same point only when it's not connected to Xcode. – ZurceZx Sep 23 '14 at 05:28
  • try mergeChanges in background thread also and did you changed all your update requests code? or only when this method is gonna called? – Saad Sep 23 '14 at 20:22
  • Changed all,I can even remove the notification when I don't need it but at the end the error it's always at ABPeoplePickerNavigationController *picker =[[ABPeoplePickerNavigationController alloc] init]; – ZurceZx Sep 24 '14 at 18:07