为什么保存被管理对象上下文变化的是Deleted值?
Why saving managed object context changes isDeleted value?
我正在使用 SwiftUI 和 Core Data 编写 iOS 应用程序。我对 Core Data 很陌生,想了解一些东西:
为什么 try self.moc.save() 从 true[=57 更改 self.item.isDeleted =] 到 false?
它发生在我删除一个 Core Data 对象之后(isDeleted 更改为 true),但稍后保存托管对象上下文将其更改为 false。这是为什么?
这是一个例子:
ContentView.swift
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
var fetchRequest: FetchRequest<Item>
var items: FetchedResults<Item> { fetchRequest.wrappedValue }
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) {item in
NavigationLink(destination: DetailsView(item: item)) {
Text("\(item.name ?? "default item name")")
}
}
}
.navigationBarTitle("Items")
.navigationBarItems(
leading:
Button(action: {
for number in 1...3 {
let item = Item(context: self.moc)
item.date = Date()
item.name = "Item \(number)"
do {
try self.moc.save()
}catch{
print(error)
}
}
}) {
Text("Add 3 items")
}
)
}
}
init() {
fetchRequest = FetchRequest<Item>(entity: Item.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Item.name, ascending: true)
])
}
}
DetailsView.swift
import SwiftUI
struct DetailsView: View {
@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var item: Item
var body: some View {
VStack {
Text("\(item.name ?? "default item name")")
}
.navigationBarItems(
trailing:
Button(action: {
self.moc.delete(self.item)
print(self.item.isDeleted)
self.presentationMode.wrappedValue.dismiss()
print(self.item.isDeleted)
do {
try self.moc.save()
print(self.item.isDeleted)
}catch{
print(error)
}
}) {
Text("Delete")
.foregroundColor(.red)
}
)
.onDisappear {
print(self.item.isDeleted)
if !self.item.isDeleted {
print(self.item.isDeleted)
self.item.name = "new name"
print(self.item.isDeleted)
do {
try self.moc.save()
}catch{
print(error)
}
}
}
}
}
我预期会发生什么:
- self.moc.delete(self.item) 将删除一个对象并将 self.item.isDeleted 标记为 true .
- 尝试self.moc.save会节省moc
- if !self.item.isDeleted 将在项目被删除时阻止代码执行(如果没有这个,我会收到一个错误:Mutating a managed object (...) after它已被删除)
没用。我在几行上添加了 print(self.item.isDeleted) 并在这些行上添加了断点以检查到底发生了什么。
事情是这样的:
- self.moc.delete(self.item) 删除了一个对象并将 self.item.isDeleted 标记为 true。
- 尝试 self.moc.save 保存 moc 并...
- self.item.isDeleted改为false
- if !self.item.isDeleted 没有阻止代码执行,因为此时 isDeleted 为 false。
这是一个错误吗?或者我不明白 Core Data 对象的生命周期和 isDeleted 应该如何变化?
Why try self.moc.save() changes self.item.isDeleted from true to
false? It happens after I delete a Core Data object (isDeleted changes
to true), but later saving managed object context changes it to false.
Why is that?
它的行为与记录的一样 - returns true before 保存,而不是在其他情况下
这是 NSManagedObject
的 Apple 文档快照:
Summary
A Boolean value that indicates whether the managed object will be
deleted during the next save. Declaration
var isDeleted: Bool { get } Discussion
true if Core Data will ask the persistent store to delete the object
during the next save operation, otherwise false. It may return false
at other times, particularly after the object has been deleted. The
immediacy with which it will stop returning true depends on where the
object is in the process of being deleted. If the receiver is a fault,
accessing this property does not cause it to fire.
我正在使用 SwiftUI 和 Core Data 编写 iOS 应用程序。我对 Core Data 很陌生,想了解一些东西:
为什么 try self.moc.save() 从 true[=57 更改 self.item.isDeleted =] 到 false? 它发生在我删除一个 Core Data 对象之后(isDeleted 更改为 true),但稍后保存托管对象上下文将其更改为 false。这是为什么?
这是一个例子:
ContentView.swift
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
var fetchRequest: FetchRequest<Item>
var items: FetchedResults<Item> { fetchRequest.wrappedValue }
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) {item in
NavigationLink(destination: DetailsView(item: item)) {
Text("\(item.name ?? "default item name")")
}
}
}
.navigationBarTitle("Items")
.navigationBarItems(
leading:
Button(action: {
for number in 1...3 {
let item = Item(context: self.moc)
item.date = Date()
item.name = "Item \(number)"
do {
try self.moc.save()
}catch{
print(error)
}
}
}) {
Text("Add 3 items")
}
)
}
}
init() {
fetchRequest = FetchRequest<Item>(entity: Item.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Item.name, ascending: true)
])
}
}
DetailsView.swift
import SwiftUI
struct DetailsView: View {
@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var item: Item
var body: some View {
VStack {
Text("\(item.name ?? "default item name")")
}
.navigationBarItems(
trailing:
Button(action: {
self.moc.delete(self.item)
print(self.item.isDeleted)
self.presentationMode.wrappedValue.dismiss()
print(self.item.isDeleted)
do {
try self.moc.save()
print(self.item.isDeleted)
}catch{
print(error)
}
}) {
Text("Delete")
.foregroundColor(.red)
}
)
.onDisappear {
print(self.item.isDeleted)
if !self.item.isDeleted {
print(self.item.isDeleted)
self.item.name = "new name"
print(self.item.isDeleted)
do {
try self.moc.save()
}catch{
print(error)
}
}
}
}
}
我预期会发生什么:
- self.moc.delete(self.item) 将删除一个对象并将 self.item.isDeleted 标记为 true .
- 尝试self.moc.save会节省moc
- if !self.item.isDeleted 将在项目被删除时阻止代码执行(如果没有这个,我会收到一个错误:Mutating a managed object (...) after它已被删除)
没用。我在几行上添加了 print(self.item.isDeleted) 并在这些行上添加了断点以检查到底发生了什么。
事情是这样的:
- self.moc.delete(self.item) 删除了一个对象并将 self.item.isDeleted 标记为 true。
- 尝试 self.moc.save 保存 moc 并...
- self.item.isDeleted改为false
- if !self.item.isDeleted 没有阻止代码执行,因为此时 isDeleted 为 false。
这是一个错误吗?或者我不明白 Core Data 对象的生命周期和 isDeleted 应该如何变化?
Why try self.moc.save() changes self.item.isDeleted from true to false? It happens after I delete a Core Data object (isDeleted changes to true), but later saving managed object context changes it to false. Why is that?
它的行为与记录的一样 - returns true before 保存,而不是在其他情况下
这是 NSManagedObject
的 Apple 文档快照:
Summary
A Boolean value that indicates whether the managed object will be deleted during the next save. Declaration
var isDeleted: Bool { get } Discussion
true if Core Data will ask the persistent store to delete the object during the next save operation, otherwise false. It may return false at other times, particularly after the object has been deleted. The immediacy with which it will stop returning true depends on where the object is in the process of being deleted. If the receiver is a fault, accessing this property does not cause it to fire.