核心数据更新无效

时间:2016-06-03 04:41:59

标签: swift core-data

我的核心数据保存,检索和删除都工作正常,但我的更新根本无法正常工作。这是我的代码:

init() {
    appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    managedContext = appDelegate.managedObjectContext
}

/**
 * Updates team in Core Data
 *
 * - parameter team: the Team to update
 */
func updateTeam(team: Team) {
    let request = NSFetchRequest(entityName: "Team")
    request.predicate = NSPredicate(format: "city = %@", team.getCity())

    do {
        if let fetchResults = try self.managedContext.executeFetchRequest(request) as? [NSManagedObject] {
            if fetchResults.count != 0 {
                let managedObject = fetchResults[0]
                managedObject.setValue(team.getCity(), forKey: "city")
                managedObject.setValue(team.getMascot(), forKey: "mascot")
                managedObject.setValue(team.getOwner(), forKey: "owner")
                managedObject.setValue(team.getManager(), forKey: "manager")
                managedObject.setValue(team.getSeasonWins(), forKey: "seasonWins")
                managedObject.setValue(team.getSeasonLosses(), forKey: "seasonLosses")
                managedObject.setValue(team.getCareerWins(), forKey: "careerWins")
                managedObject.setValue(team.getCareerLosses(), forKey: "careerLosses")

                try self.managedContext.save()
            }
        }
    } catch {
        print("Error updating user team")
    }
}

我可以保存初始团队数据,检索团队数据并删除团队数据,但是当我尝试更新团队(上面的代码)时,不会反映在以下检索中。

1 个答案:

答案 0 :(得分:-1)

确保Transient无法检查.xcdatamodel属性(对于您的所有密钥)

Transient attribute