【发布时间】:2014-11-25 22:53:08
【问题描述】:
我检查了以下问答,虽然解决方案大致相同,但问题仍然存在: Saving an updated Core Data instance
Update/Edit coreData managed object
How to update existing object in Core Data?
我的应用中有登录/注销功能。
Core Data 实体“NewLog”具有以下属性
String loginTime
String logoutTime
String loginStatus
String name
当我登录时,它会在核心数据中创建一个新对象。当我注销时,我有对象更新。问题是当我注销时它会更新现有对象但也会创建一个新对象。
这是我的超类中的“保存”代码,这样我就不必不断地重写它并冒着在我的类中出错的风险:
- (void) saveAndDismiss{
NSError * error = nil;
if ([self.managedObjectContext hasChanges]){
if (![self.managedObjectContext save: &error]){
NSLog(@"Save Failed: %@", [error localizedDescription]);
}
else {
NSLog(@"Save Successful");
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
在我的 loginViewController 实现文件中更新“loginStatus”和“logoutTime”:
- (IBAction)logOutButton:(id)sender {
//access the managed object context and create a shortcut name
NSManagedObjectContext *context = [self managedObjectContext];
//refer to the entity in the core data
NSEntityDescription *entity = [NSEntityDescription entityForName: @"NewLog" inManagedObjectContext:context];
//create a request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: entity];
// Results should be in descending order of loginTime.
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"loginTime" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
//create an array of results
NSArray *results = [context executeFetchRequest:request error:NULL];
NewLog *latestEntity = [results objectAtIndex:0];
//refer to the specific value that I wish to change
NSString * statusOfLogin = latestEntity.loginStatus;
//if the loginStatus in my app is currently YES
if ([statusOfLogin isEqual:@"YES"]){
//create a date formatter
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
//format as such
[dateformatter setDateFormat:@"dd MMM yyyy , HH:mm:ss"];
//convert to a string
NSString *dateInStringFormated=[dateformatter stringFromDate:[NSDate date]];
//hide logout button and display login button
_logOutButton.alpha = 0.0;
_loginButtonStatus.alpha = 1.0;
//status declared as String in .h file
status = @"NO";
//update the object found in "latestEntity"
latestEntity.logoutTime = dateInStringFormated;
//set the login status to be NO
latestEntity.loginStatus = status;
//BOOL declared in the .h file
logStatus = false;
self.loginLabel.text = @"Logged Out";
self.dateLabel.text = dateInStringFormated;
[super saveAndDismiss];
}
}
当我保存更新的数据时,它会创建一个仅包含 logoutTime 和 loginStatus 的新对象。
有没有办法阻止新对象的创建?
谢谢
编辑
我知道它正在通过以下方式创建一个新对象: 1. SQL 文件显示具有上述内容的新对象 - logoutTime 和 loginStatus 2. 表格视图显示新对象,并在其详细视图中显示这两个项目。
检查它来自的区域是我的选择:
//prepare for segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//first check whether the segue is equal to our transition name
if([[segue identifier]isEqualToString:@"loginSegue"])
{
//tell the segue where to go
UINavigationController *navigationController = segue.destinationViewController;
//create reference to view controller
LoginViewController *loginVC= (LoginViewController*) navigationController.topViewController;
//create the new object
NewLog *addLogDetails = [NSEntityDescription insertNewObjectForEntityForName:@"NewLog" inManagedObjectContext: [self managedObjectContext]];
loginVC.logEntry = addLogDetails;
}
}
我知道它为什么要创建一个新对象,但是如何在保持保存能力的同时防止这种情况发生?
【问题讨论】:
标签: ios objective-c iphone core-data