CoreData 创建并保存具有 nil 属性的额外实体
CoreData creates and saves extra entities with nil attributes
我有一个联系人 table 和一个位置 table,每个联系人都有很多位置
我创建一个联系人保存,然后创建一个位置并保存,然后将保存的位置分配给联系人。代码如下:
@IBAction func saveLocation(_ sender: AnyObject) {
let processName = ProcessInfo.processInfo.globallyUniqueString
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let location = Location(context: context)
do {
let fetchRequest: NSFetchRequest<Contact> = Contact.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", contactIdentifierString)
let fetchedResults = try context.fetch(fetchRequest)
if let aContact = fetchedResults.first {
// set location data
location.uniqueId = processName
location.locationName = locationNameTextField.text
location.city = self.city
location.state = self.state
location.street = self.street
location.zip = self.zip
location.country = self.country
// save data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
location.contact = aContact
myDelegate?.userSelectedContact(contactIdentifier: contactIdentifierString, locationIdentifier: processName)
}
}
catch {
print ("fetch task failed", error)
}
// Check results
do {
let fetchRequest: NSFetchRequest<Location> = Location.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", processName)
let fetchedResults = try context.fetch(fetchRequest)
if let aLocation = fetchedResults.first {
print("+++++==========++++++++++")
print("Location.Contact: \(aLocation.contact)")
print("+++++==========++++++++++")
}
}
catch {
print ("location fetch failed")
}
self.navigationController!.popViewController(animated: true)
}
我只添加了一个位置,但是当我打印联系人实体时,我看到分配了两个位置,如下所示。
Location.Contact: Optional(<rg2nrg.Contact: 0x6000002c73f0> (entity: Contact; id: 0xd000000000240000 <x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Contact/p9> ; data: {
email = vmail;
firstName = vr;
imageName = <89504e47 0d0a1a0a 0000000d 49484452 0000012c 0000012c 08060000 00797d8e 75000000 01735247 4200aece 1ce90000 0009>;
lastName = r;
locations = (
"0xd000000001780002 <x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Location/p94>",
"0xd000000001740002 <x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Location/p93>"
);
phone = 22;
providerName = tara;
screenName = vr;
uniqueId = "7B17C228-50A6-4309-BD0D-CBCD8E8FAEA1-1106-00000128477D4DB1";
userType = Household;
}))
此外,其中一个位置实体具有正确的分配属性值,而另一个位置实体具有所有 nil 值。
这弄乱了我的程序。当我循环检查位置实体属性以删除它们时,我现在看到三个位置实体
func deleteLocations() {
var fetchedResults: [Location] = []
let contextLocation = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
fetchedResults = try contextLocation.fetch(Location.fetchRequest())
print("number of locations to be deleted:: \(fetchedResults.count) ")
if fetchedResults.count > 0 {
for location in fetchedResults{
print(location.locationName)
contextLocation.delete(location)
try contextLocation.save()
print(location.locationName)
}
}
} catch {
print ("fetch contact failed")
}
}
输出:
要删除的位置数:: 3
在我的代码的后面部分,当我尝试检索值时它崩溃了,因为两个额外 Location 实体的所有属性都是 nil。
我的问题:
因为我只添加了一个 Location 实体,所以应该只有一个 Location 实体,并且应该是唯一一个链接到 Contact 的实体。它显示了分配给联系人的两个位置,然后当我计数时我有三个位置。看起来带有 nil 属性的 Location 实体不断被自己添加。
我很困惑。有人可以帮忙吗?
您很可能在某些情况下调用 saveLocation
,其中提取失败或没有结果与谓词匹配,并且 saveLocation
无法正确处理这些情况。
init(context:)
NSManagedObject
上的这个初始化程序不仅会在您调用 Location(context: context)
时在内存中创建一个空的 Location
对象,还会将该对象插入到您传递的托管对象上下文中。一旦 saveContext()
被调用,那个 Location
对象(如果不进一步编辑,它将是空的)将被持久化。
而init(context:) is not well-documented, Apple seems to have indicated that it works the same as init(entity:insertInto:)除了实体是自动算出来的。
saveLocation(:)
在这个函数中,您已经创建了 Location
对象……
let location = Location(context: context)
…在检查 Contact
是否存在并设置属性之前。
if let aContact = fetchedResults.first
因此,如果碰巧获取失败或获取结果为 0,调用初始化程序时创建的空 Location
对象将保留在上下文中,以便下次保留有人打电话给 saveContext()
.
解决与预防
在这种特殊情况下,您可能会发现移动此行后您的问题将得到解决
let location = Location(context: context)
到下面的 if let
区块:
if let aContact = fetchedResults.first {
// create location here, not at beginning of method
let location = Location(context: context)
// set location data
location.uniqueId = processName
location.locationName = locationNameTextField.text
location.city = self.city
location.state = self.state
location.street = self.street
location.zip = self.zip
location.country = self.country
// save data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
location.contact = aContact
myDelegate?.userSelectedContact(contactIdentifier: contactIdentifierString, locationIdentifier: processName)
}
作为在使用 Core Data 时防止此问题的一般规则,不要在任何 NSManagedObject
子类上使用 init(context:)
,除非您首先执行了所有必要的检查并且确定您想要项目立即插入到上下文中。
我有一个联系人 table 和一个位置 table,每个联系人都有很多位置
我创建一个联系人保存,然后创建一个位置并保存,然后将保存的位置分配给联系人。代码如下:
@IBAction func saveLocation(_ sender: AnyObject) {
let processName = ProcessInfo.processInfo.globallyUniqueString
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let location = Location(context: context)
do {
let fetchRequest: NSFetchRequest<Contact> = Contact.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", contactIdentifierString)
let fetchedResults = try context.fetch(fetchRequest)
if let aContact = fetchedResults.first {
// set location data
location.uniqueId = processName
location.locationName = locationNameTextField.text
location.city = self.city
location.state = self.state
location.street = self.street
location.zip = self.zip
location.country = self.country
// save data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
location.contact = aContact
myDelegate?.userSelectedContact(contactIdentifier: contactIdentifierString, locationIdentifier: processName)
}
}
catch {
print ("fetch task failed", error)
}
// Check results
do {
let fetchRequest: NSFetchRequest<Location> = Location.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", processName)
let fetchedResults = try context.fetch(fetchRequest)
if let aLocation = fetchedResults.first {
print("+++++==========++++++++++")
print("Location.Contact: \(aLocation.contact)")
print("+++++==========++++++++++")
}
}
catch {
print ("location fetch failed")
}
self.navigationController!.popViewController(animated: true)
}
我只添加了一个位置,但是当我打印联系人实体时,我看到分配了两个位置,如下所示。
Location.Contact: Optional(<rg2nrg.Contact: 0x6000002c73f0> (entity: Contact; id: 0xd000000000240000 <x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Contact/p9> ; data: {
email = vmail;
firstName = vr;
imageName = <89504e47 0d0a1a0a 0000000d 49484452 0000012c 0000012c 08060000 00797d8e 75000000 01735247 4200aece 1ce90000 0009>;
lastName = r;
locations = (
"0xd000000001780002 <x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Location/p94>",
"0xd000000001740002 <x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Location/p93>"
);
phone = 22;
providerName = tara;
screenName = vr;
uniqueId = "7B17C228-50A6-4309-BD0D-CBCD8E8FAEA1-1106-00000128477D4DB1";
userType = Household;
}))
此外,其中一个位置实体具有正确的分配属性值,而另一个位置实体具有所有 nil 值。
这弄乱了我的程序。当我循环检查位置实体属性以删除它们时,我现在看到三个位置实体
func deleteLocations() {
var fetchedResults: [Location] = []
let contextLocation = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
fetchedResults = try contextLocation.fetch(Location.fetchRequest())
print("number of locations to be deleted:: \(fetchedResults.count) ")
if fetchedResults.count > 0 {
for location in fetchedResults{
print(location.locationName)
contextLocation.delete(location)
try contextLocation.save()
print(location.locationName)
}
}
} catch {
print ("fetch contact failed")
}
}
输出: 要删除的位置数:: 3
在我的代码的后面部分,当我尝试检索值时它崩溃了,因为两个额外 Location 实体的所有属性都是 nil。
我的问题: 因为我只添加了一个 Location 实体,所以应该只有一个 Location 实体,并且应该是唯一一个链接到 Contact 的实体。它显示了分配给联系人的两个位置,然后当我计数时我有三个位置。看起来带有 nil 属性的 Location 实体不断被自己添加。
我很困惑。有人可以帮忙吗?
您很可能在某些情况下调用 saveLocation
,其中提取失败或没有结果与谓词匹配,并且 saveLocation
无法正确处理这些情况。
init(context:)
NSManagedObject
上的这个初始化程序不仅会在您调用 Location(context: context)
时在内存中创建一个空的 Location
对象,还会将该对象插入到您传递的托管对象上下文中。一旦 saveContext()
被调用,那个 Location
对象(如果不进一步编辑,它将是空的)将被持久化。
而init(context:) is not well-documented, Apple seems to have indicated that it works the same as init(entity:insertInto:)除了实体是自动算出来的。
saveLocation(:)
在这个函数中,您已经创建了 Location
对象……
let location = Location(context: context)
…在检查 Contact
是否存在并设置属性之前。
if let aContact = fetchedResults.first
因此,如果碰巧获取失败或获取结果为 0,调用初始化程序时创建的空 Location
对象将保留在上下文中,以便下次保留有人打电话给 saveContext()
.
解决与预防
在这种特殊情况下,您可能会发现移动此行后您的问题将得到解决
let location = Location(context: context)
到下面的 if let
区块:
if let aContact = fetchedResults.first {
// create location here, not at beginning of method
let location = Location(context: context)
// set location data
location.uniqueId = processName
location.locationName = locationNameTextField.text
location.city = self.city
location.state = self.state
location.street = self.street
location.zip = self.zip
location.country = self.country
// save data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
location.contact = aContact
myDelegate?.userSelectedContact(contactIdentifier: contactIdentifierString, locationIdentifier: processName)
}
作为在使用 Core Data 时防止此问题的一般规则,不要在任何 NSManagedObject
子类上使用 init(context:)
,除非您首先执行了所有必要的检查并且确定您想要项目立即插入到上下文中。