简体   繁体   English

ABPeoplePickerNavigationController冻结我的应用

[英]ABPeoplePickerNavigationController Freezing my app

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: 我在使用ABPeoplePickerNavigationController生成僵尸并冻结我的应用程序时遇到了麻烦,我尝试了几种方法对其进行初始化,但无论如何还是有些似乎随机冻结了我的应用程序:

  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 -Update

Here is the code I'm using to save in Core Data 这是我用来保存在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 正如我现在所说的,当连接到Xcode时,它不会冻结,但是当断开连接时,它会冻结

It's due to core data. 这是由于核心数据。 Remember, every time you initiate a change to data (eg update, delete, add objects). 记住,每次您对数据进行更改时(例如,更新,删除,添加对象)。 Make a new ManagedObjectContext with the same persistentStoreCoordinator. 使用相同的persistentStoreCoordinator创建一个新的ManagedObjectContext。 And add Didsave notification observer to the core data. 并将Didsave通知观察器添加到核心数据。 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. 并假设您的线程1向实体1请求数据,同时线程2向实体1添加数据,线程3从入口1删除数据。 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 StackoverFlow详细信息

A blog with code examples 一个带有代码示例的博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM