2014-09-22 152 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