So the following CoreData can also be written as SwiftData?
func fetchPersistentHistory() {
let context = persistentContainer.newBackgroundContext()
context.perform {
let lastToken = self.loadLastToken()
let request = NSPersistentHistoryChangeRequest.fetchHistory(after: lastToken)
if let result = try? context.execute(request) as? NSPersistentHistoryResult,.
let transactions = result.result as? [NSPersistentHistoryTransaction] {
Post
Replies
Boosts
Views
Activity
Thanks Ziqiao Chen.
So the role is the same as NSPersistentHistoryTransaction. So we can use historical information to manage history by saving and restoring tokens?
Thanks! It seems there was a problem with the timer and the content of the part that outputs the numbers after all. Until now, the problem was hidden by @Published and I didn't seem to notice it.
Thanks!
It seems there was a problem with the timer and the content of the part that outputs the numbers after all. Until now, the problem was hidden by @Published and I didn't seem to notice it.
@Observable
class ViewModel {
static let shared = ViewModel()
var hours: Int = 0
var minutes: Int = 0
var seconds: Int = 0
func testTimer() {
print("init")
Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { _ in
self.seconds += 1
if self.seconds == 60 {
self.minutes += 1
if self.minutes == 60 {
self.hours += 1
}
}
}
}
}
Thanks for answering. Indeed, it is working as it should. However, it does not work when it is closer to the actual method used as shown below, is this the wrong way to do it?