2014-09-22 109 views
1

我遇到的ABPeoplePickerNavigationController生成僵尸和冻结我的应用程序的麻烦,我尝试了几种方法来初始化,但好歹还是另一个似乎会随机冻结我的应用程序:的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]; 

     }); 


    } 

我不知道我做错了什么,但这是冻结我的应用程序outsite模拟器或当它没有调试的设备。 任何想法为什么?

-Update

这里是我使用的核心数据

#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

你在你的应用程序使用coredata? – Saad 2014-09-22 22:04:58

+0

为什么你需要互联网来选择联系人? – rmaddy 2014-09-22 22:05:16

+0

您是否尝试过使用调试器来查看您的应用“冻结”的位置? – rmaddy 2014-09-22 22:05:33

回答

0

这是由于核心数据。请记住,每次启动对数据的更改(例如更新,删除,添加对象)。使用相同的persistentStoreCoordinator创建一个新的ManagedObjectContext。并将Didsave通知观察者添加到核心数据。基本上发生的是核心数据不是线程安全的。假设你的thread1向entity1请求数据,同时thread2向entity1和thread3添加数据,从entry1删除数据。由于不管理并发,执行将会停止。因为您需要研究这些链接以便更好地理解并查看示例代码。

Apple Developer Concurrency with Core Data

Sample Code Apple Developer

StackoverFlow Detail

A blog with code examples

+0

嗨!巨大的回应,但仍然是同样的问题,我改变了我在Core Data中保存数据的方式,现在只有当它没有连接到Xcode时,它才冻结在同一点。 – ZurceZx 2014-09-23 05:28:18

+0

编写核心数据的代码 – Saad 2014-09-23 07:43:36

+0

更新了原始帖子,代码为 – ZurceZx 2014-09-23 15:57:57