2016-03-12 2 views
1

J'ai une entité dans CoreDate appelée BudgetDetail. J'ai le code ci-dessous que je veux mettre à jour l'attribut 'restant' mais il semble ajouter un nouvel enregistrement plutôt que de mettre à jour.remplacement d'attribut dans CoreData

func checksBudgetDetail(budgetName: String, spendAmount: Double, date: NSDate){ 
     print("budget name = \(budgetName)") 

     //fetch results fron income entity and place in array 
     let budgetDetailResults: [AnyObject] = try! context.executeFetchRequest(budgetDetail) 
    print("budget results loaded") 

     //if there are no incomes saved, do nothing. 
     if budgetDetailResults.isEmpty { 
     } 

     //if there are incomes saved, retrieve name and amount 
     else { 
      for i in budgetDetailResults { 
       print("in loop") 

       let name = (i.valueForKey(budgetNameKeyToUpdate)! as! String) 
       let amount = Double(i.valueForKey(budgetAmountKeyToUpdate)!.stringValue) 
       let durationType = (i.valueForKey(budgetDurationTypeKeyToUpdate)! as! String) 
       let durationOccurance = Double(i.valueForKey(budgetDurationOccuranceKeyToUpdate)!.stringValue) 
       let remaining = (i.valueForKey(budgetRemaningKeyToUpdate)!.stringValue) 
       let startDate = (i.valueForKey(startDateKeyToUpdate)! as! NSDate) 
       let endDate = (i.valueForKey(endDateKeyToUpdate)! as! NSDate) 

       if name == budgetName { 
        print("just logged a spend against the budget \(name)") 

        //if the date in the on the spend is within the startDate and endDate, call the updateBudgetDetailEntity to update the remaining budget 
        if startDate.compare(date) == .OrderedAscending && endDate.compare(date) == .OrderedDescending { 

         print("the spend which has just been logged is also within the dates of \(name)'s budget") 

         let newRemaining : Double 

         print("\(budgetName) has a old reamining amount of:\(remaining)") 

         newRemaining = Double(remaining)! - spendAmount 

         print("\(budgetName) has a new reamining amount of:\(newRemaining)") 

         let entity = NSEntityDescription.entityForName("BudgetDetail", inManagedObjectContext: context)! 
         let saveBudgetEntity = NSManagedObject(entity: entity, insertIntoManagedObjectContext:context) 

         // add user input to the relevant entity 
         saveBudgetEntity.setValue(name, forKey: budgetNameKeyToUpdate) 
         saveBudgetEntity.setValue(amount, forKey: budgetAmountKeyToUpdate) 
         saveBudgetEntity.setValue(durationType, forKey: budgetDurationTypeKeyToUpdate) 
         saveBudgetEntity.setValue(durationOccurance, forKey: budgetDurationOccuranceKeyToUpdate) 
         saveBudgetEntity.setValue(startDate, forKey: startDateKeyToUpdate) 
         saveBudgetEntity.setValue(endDate, forKey: endDateKeyToUpdate) 
         saveBudgetEntity.setValue(newRemaining, forKey: budgetRemaningKeyToUpdate) 


        do { 
         try context.save() 
         print("new remaining attriute saved") 
        } catch _ { 
         print("new remaining attriute didnt saved") 
        } 


        } 
        else { 
         //would need to create a new record with new start and end dates 
        } 
       } 
       else { 

       } 

       print("\(date)") 
       print("Name:\(budgetName), startDate:\(startDate), endDate:\(endDate)") 
      } 
     } 

} 

Il semble qu'un nouvel enregistrement a été ajouté plutôt que de mettre à jour celui qui se trouve dans la boucle. Je ne sais pas pourquoi cela se passe et toute aide serait appréciée.

Répondre

1

Ces lignes:

let entity = NSEntityDescription.entityForName("BudgetDetail", inManagedObjectContext: context)! 
let saveBudgetEntity = NSManagedObject(entity: entity, insertIntoManagedObjectContext:context) 
// add user input to the relevant entity 
saveBudgetEntity.setValue(name, forKey: budgetNameKeyToUpdate) 
saveBudgetEntity.setValue(amount, forKey: budgetAmountKeyToUpdate) 
saveBudgetEntity.setValue(durationType, forKey: budgetDurationTypeKeyToUpdate) 
saveBudgetEntity.setValue(durationOccurance, forKey: budgetDurationOccuranceKeyToUpdate) 
saveBudgetEntity.setValue(startDate, forKey: startDateKeyToUpdate) 
saveBudgetEntity.setValue(endDate, forKey: endDateKeyToUpdate) 
saveBudgetEntity.setValue(newRemaining, forKey: budgetRemaningKeyToUpdate) 

créer un nouvel objet géré et définir ses valeurs d'attribut. Si vous voulez simplement mettre à jour l'objet existant, supprimez ces lignes et remplacez par:

i.setValue(newRemaining, forKey: budgetRemaningKeyToUpdate) 
+0

Parfait merci! –