我遇到麻烦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];
});
}
我不知道我做错了什么,但这会冻结我的应用程序在模拟器外部或当设备没有调试时。 知道为什么吗?
-Update
以下是我用于保存在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时它不会冻结,但是当它断开连接时它会
答案 0 :(得分:0)
由于核心数据。请记住,每次启动数据更改(例如更新,删除,添加对象)。使用相同的persistentStoreCoordinator创建一个新的ManagedObjectContext。并将Didsave通知观察器添加到核心数据中。基本上发生的是核心数据不是线程安全的。假设你的thread1要求来自entity1的数据,同时thread2将数据添加到entity1,而thread3从entry1中删除数据。由于未管理并发,因此将停止执行。因为您要学习这些链接以便更好地理解并查看示例代码。