为什么我的核心数据保存缓慢?

时间:2013-01-01 20:45:41

标签: ios uitableview core-data nsmanagedobjectcontext

每次我尝试保存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];
        }
    }
}

代码只保存所有textFields文本。它这么慢的原因是什么?非常感谢任何帮助。

1 个答案:

答案 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];
    }
}

如果您想监控Core Data,您应该通过Instrument进行监控。此外,您可以按如下方式设置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

希望有所帮助。