1

我在 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];

        });


    }

我不知道我做错了什么,但这会将我的应用程序冻结在模拟器之外,或者当设备没有调试时。知道为什么吗?

-更新

这是我用来保存在 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");

    });
}

正如我现在所说,在运行连接到 Xcode 时它不会冻结,但在断开连接时它会冻结

4

1 回答 1

0

这是由于核心数据。请记住,每次您发起对数据的更改(例如更新、删除、添加对象)时。使用相同的 persistentStoreCoordinator 创建一个新的 ManagedObjectContext。并将 Didsave 通知观察者添加到核心数据中。基本上发生的是核心数据不是线程安全的。假设您的线程 1 要求来自实体 1 的数据,同时线程 2 向实体 1 添加数据,线程 3 从条目 1 中删除数据。由于未管理并发,因此将停止执行。对您来说,研究这些链接以更好地理解并查看示例代码。

Apple Developer 与 Core Data 的并发性

示例代码 Apple Developer

StackoverFlow 详细信息

包含代码示例的博客

于 2014-09-23T00:58:28.407 回答