我在 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 时它不会冻结,但在断开连接时它会冻结