import Foundation
import CoreData
/**
ヘルスケアデータを管理するコントローラ
*/
class HealthDataPersistenceController {
var container: NSPersistentContainer
// var dataSource: [[String:String]] = []
var idArray: [String] = []
let healthDataEntityName = "HealthData"
init() {
// ModelファイルにつけたHealthDataModelを指定する
container = NSPersistentContainer(name: "HealthDataModel")
// ContainerからStoreを読み込む処理を記述する
container.loadPersistentStores { (storeDescription, error) in
if let error = error {
print("error:\(error))")
}
}
}
// ヘルスケアデータを追加する
func addHealthData(sampleData: HealthDataTypeValue) {
let context = container.viewContext
let entity = NSEntityDescription.entity(forEntityName: healthDataEntityName, in: context)!
let newHealthData = NSManagedObject(entity: entity, insertInto: context)
newHealthData.setValue(sampleData.id, forKey: HealthDataKey.id)
newHealthData.setValue(sampleData.startDate, forKey: HealthDataKey.startDate)
newHealthData.setValue(sampleData.endDate, forKey: HealthDataKey.endDate)
newHealthData.setValue(sampleData.sampleType, forKey: HealthDataKey.sampleType)
newHealthData.setValue(sampleData.value, forKey: HealthDataKey.value)
do {
try context.save()
print("saveData success")
} catch {
print("addHealthData save error:\(error)")
}
}
// 全てのデータを取得する
func getData() -> [[String:String]]? {
let context = container.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: healthDataEntityName)
var dataSource: [[String:String]] = []
do {
let result = try context.fetch(fetchRequest)
for data in result {
let id = data.value(forKey: HealthDataKey.id) as! String
let startDate = data.value(forKey: HealthDataKey.startDate) as! String
let endDate = data.value(forKey: HealthDataKey.endDate) as! String
let sampleType = data.value(forKey: HealthDataKey.sampleType) as! String
let value = data.value(forKey: HealthDataKey.value) as! String
idArray.append(id)
let newData = [
"id": id,
"startDate": startDate,
"endDate": endDate,
"sampleType": sampleType,
"value": value
]
print("newData:\(newData)")
dataSource.append(newData)
}
return dataSource
} catch {
print("getData error:\(error)")
return nil
}
}
// func updateData(sampleData: HealthData) {
// let context = container.viewContext
// let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "HealthData")
// do {
// let result = try context.fetch(fetchRequest)
// print("result:\(result)")
// if let firstObject = result.first as? NSManagedObject {
// firstObject.setValue(sampleData.id, forKey: "id")
// firstObject.setValue(sampleData.startDate, forKey: "startDate")
// firstObject.setValue(sampleData.endDate, forKey: "endDate")
// firstObject.setValue(sampleData.sampleType, forKey: "sampleType")
// firstObject.setValue(sampleData.value, forKey: "value")
// }
// } catch {
// print("update data error")
// }
//
// do {
// try context.save()
// print("updateData success")
// } catch {
// print("save error")
// }
// }
// データを削除する
func deleteData(sampleData: [[String:String]]) {
let context = container.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: healthDataEntityName)
do {
let result = try context.fetch(fetchRequest)
for object in result {
context.delete(object)
}
} catch {
print("deleteData error:\(error)")
}
do {
try context.save()
print("deleteData save success")
} catch {
print("deleteData save error:\(error)")
}
}
// idでデータを削除する
func deleteDataById(id: String) {
let context = container.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: healthDataEntityName)
fetchRequest.predicate = NSPredicate(format: "id = %@", id)
do {
let result = try context.fetch(fetchRequest)
for object in result {
context.delete(object)
}
idArray = idArray.filter { $0 != id }
print("idArray:\(idArray)")
} catch {
print("deleteDataById error:\(error)")
}
do {
try context.save()
print("deleteDataById save success")
} catch {
print("deleteDataById save error:\(error)")
}
}
}
Core Data相关
于 2024-01-19 09:06:47 首次发布