当我想保存核心数据时不保存
core data not saving when I want to
我正在开发一个保存医疗记录的应用程序。我正在使用具有单一上下文和单一实体商店的核心数据。尽管调用 [managedObjectContext save:&error],如果我通过 Xcode 提取容器,sqlite 数据库不会显示最新数据。
尝试了此处建议的方法:Core data not saving my data,但对我来说没有效果。
更糟糕的是,数据确实会在一段时间后出现:应用程序可以通过 FTP 发送 sqlite 以转储数据以进行灾难恢复。碰巧上周的工作价值并不总是出现,但一周后通常会出现。
我不明白的是:[managedObjectContext save:&error] 是否立即将数据写入物理存储? (我的猜测是不)
我如何告诉核心数据我希望我的数据当场写下来(前提是它确实可能)?
关闭应用程序 and/or 重启设备是否会使核心数据写入 sqlite?
编辑 1 - 添加一些代码
[managedObjectContext save:&error] 在 [CommonHelper SaveData:true] 中被调用;就在@try 块的开头。
_FollowUp.bMI=[NSNumber numberWithFloat: [TextBMI.text floatValue]];
_FollowUp.karfnosky=[NSNumber numberWithInt: [TextKarfnosky.text intValue]];
_FollowUp.charlson=[NSNumber numberWithInt: [TextCharlson.text intValue]];
_FollowUp.alimentazione=ButtonAlimentazione.titleLabel.text;
_FollowUp.farmaci=TextFarmaci.text;
_FollowUp.dolore=ButtonDolore.titleLabel.text;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
@try {
[CommonHelper SaveData:true];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@“Data saved” message: @“Record saved successfully” delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
float height = self.view.bounds.size.height;
float width = self.view.bounds.size.width;
lblSincro = [[UILabel alloc]initWithFrame:CGRectMake(width/2-200 ,height/2+30, 400, 20)];
lblSincro.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lblSincro.font = [UIFont boldSystemFontOfSize:16.0f];
lblSincro.numberOfLines = 1;
[lblSincro setTextAlignment:NSTextAlignmentCenter];
lblSincro.backgroundColor = [UIColor clearColor];
lblSincro.textColor = [UIColor whiteColor];
lblSincro.text = @"Uploading in progress";
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] ;
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] ;
spinner.frame = CGRectMake(0, 0, width,height);
spinner.tag = 3;
UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
spinner.backgroundColor =color;
lblSincroDet = [[UILabel alloc]initWithFrame:CGRectMake(width/2-200 ,height/2+70, 400, 20)];
lblSincroDet.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lblSincroDet.font = [UIFont boldSystemFontOfSize:16.0f];
lblSincroDet.numberOfLines = 1;
[lblSincroDet setTextAlignment:NSTextAlignmentCenter];
lblSincroDet.backgroundColor = [UIColor clearColor];
lblSincroDet.textColor = [UIColor whiteColor];
[self.view addSubview:lblSincro];
[self.view addSubview:lblSincroDet];
[self.view addSubview:spinner];
[spinner addSubview:lblSincro];
[spinner addSubview:lblSincroDet];
[spinner startAnimating];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
SyncHelper *sync=[SyncHelper alloc];
BOOL result=sync.Sincronize;
dispatch_async(dispatch_get_main_queue(), ^(void){
[spinner stopAnimating];
[lblSincro removeFromSuperview];
[lblSincroDet removeFromSuperview];
[lblSincro removeFromSuperview];
if(result){
_SaveView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"successSincro", @"") message:NSLocalizedString(@"successSincroMessage", @"") delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[_SaveView show];
}else{
_SaveView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"failedSincro", @"") message:NSLocalizedString(@"failedSincroMessage", @"")
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[_SaveView show];
}
});
});
_FollowUpLoad=_FollowUp;
_isNew=false;
}@catch (NSException *ex)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @“Failed to save data” delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
你必须根据他们保存的线程访问 NSManagedObjectContext。
读这个 link.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/Concurrency.html
将您的数据插入主队列
dispatch_async(dispatch_get_main_queue(), ^{
[CommonHelper SaveData:true];
});
希望一切顺利。
CoreData 默认使用 Write-Ahead Logging,因此 .sqlite 文件不会始终包含您的所有数据,除非您禁用它。
像这样...
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}
};
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]
我正在开发一个保存医疗记录的应用程序。我正在使用具有单一上下文和单一实体商店的核心数据。尽管调用 [managedObjectContext save:&error],如果我通过 Xcode 提取容器,sqlite 数据库不会显示最新数据。
尝试了此处建议的方法:Core data not saving my data,但对我来说没有效果。 更糟糕的是,数据确实会在一段时间后出现:应用程序可以通过 FTP 发送 sqlite 以转储数据以进行灾难恢复。碰巧上周的工作价值并不总是出现,但一周后通常会出现。
我不明白的是:[managedObjectContext save:&error] 是否立即将数据写入物理存储? (我的猜测是不) 我如何告诉核心数据我希望我的数据当场写下来(前提是它确实可能)? 关闭应用程序 and/or 重启设备是否会使核心数据写入 sqlite?
编辑 1 - 添加一些代码 [managedObjectContext save:&error] 在 [CommonHelper SaveData:true] 中被调用;就在@try 块的开头。
_FollowUp.bMI=[NSNumber numberWithFloat: [TextBMI.text floatValue]];
_FollowUp.karfnosky=[NSNumber numberWithInt: [TextKarfnosky.text intValue]];
_FollowUp.charlson=[NSNumber numberWithInt: [TextCharlson.text intValue]];
_FollowUp.alimentazione=ButtonAlimentazione.titleLabel.text;
_FollowUp.farmaci=TextFarmaci.text;
_FollowUp.dolore=ButtonDolore.titleLabel.text;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
@try {
[CommonHelper SaveData:true];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@“Data saved” message: @“Record saved successfully” delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
float height = self.view.bounds.size.height;
float width = self.view.bounds.size.width;
lblSincro = [[UILabel alloc]initWithFrame:CGRectMake(width/2-200 ,height/2+30, 400, 20)];
lblSincro.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lblSincro.font = [UIFont boldSystemFontOfSize:16.0f];
lblSincro.numberOfLines = 1;
[lblSincro setTextAlignment:NSTextAlignmentCenter];
lblSincro.backgroundColor = [UIColor clearColor];
lblSincro.textColor = [UIColor whiteColor];
lblSincro.text = @"Uploading in progress";
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] ;
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] ;
spinner.frame = CGRectMake(0, 0, width,height);
spinner.tag = 3;
UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
spinner.backgroundColor =color;
lblSincroDet = [[UILabel alloc]initWithFrame:CGRectMake(width/2-200 ,height/2+70, 400, 20)];
lblSincroDet.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lblSincroDet.font = [UIFont boldSystemFontOfSize:16.0f];
lblSincroDet.numberOfLines = 1;
[lblSincroDet setTextAlignment:NSTextAlignmentCenter];
lblSincroDet.backgroundColor = [UIColor clearColor];
lblSincroDet.textColor = [UIColor whiteColor];
[self.view addSubview:lblSincro];
[self.view addSubview:lblSincroDet];
[self.view addSubview:spinner];
[spinner addSubview:lblSincro];
[spinner addSubview:lblSincroDet];
[spinner startAnimating];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
SyncHelper *sync=[SyncHelper alloc];
BOOL result=sync.Sincronize;
dispatch_async(dispatch_get_main_queue(), ^(void){
[spinner stopAnimating];
[lblSincro removeFromSuperview];
[lblSincroDet removeFromSuperview];
[lblSincro removeFromSuperview];
if(result){
_SaveView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"successSincro", @"") message:NSLocalizedString(@"successSincroMessage", @"") delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[_SaveView show];
}else{
_SaveView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"failedSincro", @"") message:NSLocalizedString(@"failedSincroMessage", @"")
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[_SaveView show];
}
});
});
_FollowUpLoad=_FollowUp;
_isNew=false;
}@catch (NSException *ex)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @“Failed to save data” delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
你必须根据他们保存的线程访问 NSManagedObjectContext。 读这个 link.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/Concurrency.html
将您的数据插入主队列
dispatch_async(dispatch_get_main_queue(), ^{
[CommonHelper SaveData:true];
});
希望一切顺利。
CoreData 默认使用 Write-Ahead Logging,因此 .sqlite 文件不会始终包含您的所有数据,除非您禁用它。
像这样...
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}
};
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]