在iOS中,要刷新或更新Core Data中的瞬态属性,您可以遵循以下步骤:
myManagedObject.lastAccessedDate = Date()
do {
try managedObjectContext.save()
} catch {
print("Error saving context: \(error)")
}
NotificationCenter.default.addObserver(self, selector: #selector(contextDidSave(_:)), name: NSNotification.Name.NSManagedObjectContextDidSave, object: managedObjectContext)
然后,实现contextDidSave(_:)方法:
@objc func contextDidSave(_ notification: Notification) {
guard let context = notification.object as? NSManagedObjectContext else { return }
if context !== managedObjectContext {
managedObjectContext.performAndWait {
do {
try managedObjectContext.mergeChanges(fromContextDidSave: notification)
} catch {
print("Error merging changes: \(error)")
}
}
}
}
通过遵循这些步骤,您可以在iOS应用程序中刷新或更新Core Data瞬态属性。
领取专属 10元无门槛券
手把手带您无忧上云