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

标签 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 文本。它这么慢的原因是什么?非常感谢任何帮助。

最佳答案

根据你的问题很难理解发生了什么,但我会修改 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];
    }
}

如果你想监控核心数据,你应该通过 Instrument 来完成。此外,您可以按如下方式设置 Xcode:XCode4 and Core Data: How to enable SQL Debugging .

编辑

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

Core Data - Storing Images (iPhone)

编辑2

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

否则,如果 iOS 5 是最低要求,请使用外部存储标志。 How to enable External Storage for Binary Data in Core Data

希望对您有所帮助。

关于ios - 为什么我的核心数据保存缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14114060/

相关文章:

ios - 如何匹配两个语音文件并返回是否是同一个词?

ios - GADBannerView 滚动与 UITableView IOS

ios - 当数据模型实体超过3个时,Xcode构建会挂起

ios - UITableViewController 以编程方式访问静态单元格问题

ios - 填充嵌入式 UITableViewController

swift - CoreData 实体和属性

objective-c - 核心数据 : How to fetch a specific object using a predicate

ios - 如何在 iOS 中创建带有渐变的叠加颜色

ios - 从自定义类更改 View

ios - 将输入数字存储在文本框中的变量中