gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:04:54 25 4
gpt4 key购买 nike

每次我尝试保存我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com