【问题标题】:Why is my core data saving slow?为什么我的核心数据保存缓慢?
【发布时间】:2013-01-01 20:45:41
【问题描述】:

每次我尝试保存我的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 文本。它这么慢的原因是什么?非常感谢任何帮助。

【问题讨论】:

  • 您是否使用 Instrument 来记录经过的时间?提供更多详细信息。

标签: ios uitableview core-data nsmanagedobjectcontext


【解决方案1】:

根据您的问题很难理解发生了什么,但我将修改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

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

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

希望对您有所帮助。

【讨论】:

  • 感谢您的帮助,我认为保存速度慢的原因是a.gunImage = self.image.image; 保存图像时出现问题。我的数据模型中的gunImage 属性为transformable,并且我正在使用 NSValueTransformer 类来序列化/反序列化图像并将其保存到核心数据。但由于某种原因,它变得非常缓慢!
  • @Bad_APPZ 不客气。图片有多大?我已经为你添加了一个编辑。还修正了语法。 ;)
  • 我怎样才能知道它有多大?我只允许用户从照片库中选择,或者用相机拍照!
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多