Created
February 24, 2023 07:11
-
-
Save katsuyoshi/112e23c0632a52d7219705db9b05967e to your computer and use it in GitHub Desktop.
CoreDataのwillSave()を使う際はsetPrimitiveValue(:forkey:)を使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import CoreData | |
struct ContentView: View { | |
@Environment(\.managedObjectContext) private var viewContext | |
@FetchRequest( | |
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], | |
animation: .default) | |
private var items: FetchedResults<Item> | |
var body: some View { | |
NavigationView { | |
List { | |
ForEach(items) { item in | |
NavigationLink { | |
Text("Item at \(item.timestamp!, formatter: itemFormatter)") | |
} label: { | |
VStack { | |
HStack { | |
Text("Timestamp") | |
Spacer() | |
Text(item.timestamp!, formatter: itemFormatter) | |
} | |
HStack { | |
Text("Created at") | |
Spacer() | |
if let t = item.createdAt { | |
Text(t, formatter: itemFormatter) | |
} | |
} | |
HStack { | |
Text("Updated at") | |
Spacer() | |
if let t = item.updatedAt { | |
Text(t, formatter: itemFormatter) | |
} | |
} | |
} | |
} | |
} | |
.onDelete(perform: deleteItems) | |
} | |
.toolbar { | |
ToolbarItem(placement: .navigationBarTrailing) { | |
EditButton() | |
} | |
ToolbarItem { | |
Button(action: addItem) { | |
Label("Add Item", systemImage: "plus") | |
} | |
} | |
} | |
Text("Select an item") | |
} | |
} | |
private func addItem() { | |
withAnimation { | |
let newItem = Item(context: viewContext) | |
newItem.timestamp = Date() | |
do { | |
try viewContext.save() | |
} catch { | |
// Replace this implementation with code to handle the error appropriately. | |
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
let nsError = error as NSError | |
fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
} | |
} | |
} | |
private func deleteItems(offsets: IndexSet) { | |
withAnimation { | |
offsets.map { items[$0] }.forEach(viewContext.delete) | |
do { | |
try viewContext.save() | |
} catch { | |
// Replace this implementation with code to handle the error appropriately. | |
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
let nsError = error as NSError | |
fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
} | |
} | |
} | |
} | |
private let itemFormatter: DateFormatter = { | |
let formatter = DateFormatter() | |
formatter.dateStyle = .short | |
formatter.timeStyle = .medium | |
return formatter | |
}() | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CoreData | |
@objc(Item) | |
public class Item: NSManagedObject { | |
override public func willSave() { | |
super.willSave() | |
let now = Date() | |
if createdAt == nil { | |
setPrimitiveValue(now, forKey: "createdAt") | |
} | |
setPrimitiveValue(now, forKey: "updatedAt") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CoreData | |
extension Item { | |
@nonobjc public class func fetchRequest() -> NSFetchRequest<Item> { | |
return NSFetchRequest<Item>(entityName: "Item") | |
} | |
@NSManaged public var timestamp: Date? | |
@NSManaged public var createdAt: Date? | |
@NSManaged public var updatedAt: Date? | |
} | |
extension Item : Identifiable { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment