我有檢查以下是問答& A和而解決方案是很多問題仍然對我來說同樣存在: Saving an updated Core Data instance更新對象值也創造了新的對象
Update/Edit coreData managed object
How to update existing object in 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 。tableview顯示新對象,並在詳細視圖中顯示這兩個項目。
檢查它是從我賽格瑞未來區域:
//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;
}
}
我知道爲什麼它創建一個新的對象,但如何防止這種情況的發生,同時保持能夠拯救?
我知道它正在創建一個新對象,因爲我每次運行和測試時都檢查SQL文件,並且tableview還顯示新對象以及顯示詳細信息的詳細視圖,如我所說:「當我保存更新的數據它會創建一個只包含logoutTime和loginStatus的新對象。「否則我不會發布這個問題。雖然我會嘗試使用awakeFromInsert的建議。 – 2014-10-01 07:36:09
當使用核心數據時,如果不是您的引用點,則獲取並檢查count sql文件 – amar 2014-10-01 07:47:03
發現segue是我的下一個視圖控制器類中的問題。我知道爲什麼它現在正在創建一個新的對象,但是如何在保留更新的信息的同時阻止它呢? – 2014-10-01 08:11:21