2013-01-01 109 views
1

每次我嘗試保存我的NSManagedObjectContex時,每次需要1-8秒。這裏是我的代碼:爲什麼我的核心數據保存速度很慢?

- (IBAction)save 
{  
    if (self.managedObjectContext == nil) { 
     self.managedObjectContext = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    } 

    if (self.brandText.text.length == 0 || self.modelText.text.length == 0) { 
     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please fill out the required fields" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(125, 40, 31, 7)]; 

     NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bullet.png"]]; 
     UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path]; 
     [imageView setImage:bkgImg]; 

     [alertView addSubview:imageView]; 

     [alertView show]; 

    } else { 
     Hand *a = [NSEntityDescription insertNewObjectForEntityForName:@"Hand" inManagedObjectContext:self.managedObjectContext]; 
     a.brand = self.brandText.text; 
     a.caliber = self.cText.text; 
     self.managedObjectContext = self.app.managedObjectContext; 
     a.notes = self.notesView.text; 
     a.serialNumber = self.serialNumberText.text; 
     a.nickname = self.nicknameText.text; 
     a.model = self.modelText.text; 
     a.gunImage = self.image.image; 
     a.roundsFired = [NSString stringWithFormat:@"0"]; 
     a.cleanRounds = [NSString stringWithFormat:@"500"]; 
     a.showBadge = [NSNumber numberWithBool:YES]; 
     [self dismissViewControllerAnimated:YES completion:nil]; 

     NSError *error;  
     if (![self.managedObjectContext save:&error]) { 
      UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an internal error. \n Please restart the app and try again, Thank You" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; 
      [errorAlert show]; 
     } 
    } 
} 

代碼只是保存所有的textField文本。它如此緩慢的原因是什麼?任何幫助是極大的讚賞。

+0

您是否使用儀器記錄經過時間?提供更多細節。 –

回答

2

根據您的問題是相當困難的理解是怎麼回事,但我會如下修改else聲明...

else { 
    Hand *a = [NSEntityDescription insertNewObjectForEntityForName:@"Hand" inManagedObjectContext:self.managedObjectContext]; 
    a.brand = self.brandText.text; 
    a.caliber = self.cText.text; 
    // why this? 
    // self.managedObjectContext = self.app.managedObjectContext; 
    a.notes = self.notesView.text; 
    a.serialNumber = self.serialNumberText.text; 
    a.nickname = self.nicknameText.text; 
    a.model = self.modelText.text; 
    a.gunImage = self.image.image; 
    a.roundsFired = [NSString stringWithFormat:@"0"]; 
    a.cleanRounds = [NSString stringWithFormat:@"500"]; 
    a.showBadge = [NSNumber numberWithBool:YES];   

    NSError *error;  
    if (![self.managedObjectContext save:&error]) { // error during saving 
     UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an internal error. \n Please restart the app and try again, Thank You" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; 
     [errorAlert show]; 
    } else { // the save completes correctly, dismiss the view controller 
     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 
} 

如果你想監控的核心數據,你應該通過儀器做。另外,您可以按如下方式設置Xcode:XCode4 and Core Data: How to enable SQL Debugging

編輯

由於節省緩慢是由於(根據您的評論)影像外,應該依賴於這些規則。

Core Data - Storing Images (iPhone)

編輯2

好吧,既然你不事先知道你的圖像的大小(以字節爲單位),我真的建議的圖像存儲在文件系統,而不是在Core Data存儲中。在數據庫中只保存圖像的路徑。圖像將保存在後臺。這可以防止UI被阻止。

否則,如果iOS 5是最低要求,請使用外部存儲標誌。 How to enable External Storage for Binary Data in Core Data

希望有所幫助。

+0

謝謝你的幫助,我已經把保存速度緩慢的原因歸結於'a.gunImage = self.image.image;'它在保存圖像時遇到了問題。我在我的數據模型中具有'gunImage'屬性作爲'transformable',我使用NSValueTransformer類對圖像進行序列化/反序列化並將其保存爲核心數據。但由於某種原因,它速度很慢! –

+1

@Bad_APPZ不客氣。圖像有多大?我爲你添加了一個修改。還修復了語法。 ;) –

+0

我怎麼知道它有多大?我只允許用戶從照片庫中選擇它,或者用相機拍攝! –

相關問題