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

该代码仅保存所有 textFields 文本。它这么慢的原因是什么?任何帮助是极大的赞赏。

4

1 回答 1

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 和核心数据:如何启用 SQL 调试

编辑

由于图像保存缓慢(根据您的评论),因此您应该依赖这些规则。

核心数据 - 存储图像 (iPhone)

编辑 2

好的,由于您事先不知道图像的大小(以字节为单位),我真的建议将图像存储在文件系统中,而不是存储在核心数据存储中。在数据库中只保存图像的路径。图像将保存在背景中。这是为了防止 UI 阻塞。

否则,如果 iOS 5 是最低要求,请使用外部存储标志。 如何在 Core Data 中为二进制数据启用外部存储

希望有帮助。

于 2013-01-01T21:08:25.513 回答