这是我的问题。
我正在使用Xcode 7.3.1上的Core Data,Swift 2。我有3张桌子。第一个名为TableX,下一个TableY,最后是TableZ,每个人都有自己的属性。 TableX没有任何关系,TableY与TableX的关系是“To Many”而没有反向;并且TableZ与TableY和反向具有“To Many”的关系。之后,我生成了我的NSManagedObject子类。
我在TableX中添加了4个元素,然后在TableY中添加了1个与TableX中的4个元素相关的寄存器,然后在TableZ中添加了2个元素,每个元素与TableY中的寄存器相关。当我这样做时,TableX中的元素不出现在TableZ的第一个寄存器中,但是它们出现在第二个寄存器中,TableZ中的两个寄存器都正确地保存了TableY中的寄存器。为什么会这样?我添加了我的代码。
func addElementToTableX(idEleX: String)->TableX?{
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequestTableX = NSFetchRequest(entityName: "TableX")
let predicateTableX = NSPredicate(format: "tablexid == %@", idEleX)
fetchRequestTableX.predicate = predicateTableX
do {
let fetchedEntitiesTableX = try managedObjectContext.executeFetchRequest(fetchRequestTableX) as! [TableX]
if (fetchedEntitiesTableX.count == 0){
let newTableX = NSEntityDescription.insertNewObjectForEntityForName("TableX", inManagedObjectContext: managedObjectContext) as! TableX
newTableX.tablexid = "saldkjadkj"
do {
try newTableX.managedObjectContext!.save()
print("Saved!")
return newTableX
} catch let error as NSError {
print("Error")
return nil
}
}else{
print("This element exist")
return fetchedEntitiesTableX[0]
}
} catch {
return nil
}
}
func addElementToTableY(idEleY: String, elementsTableX: [TableX])->TableY?{
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequestTableY = NSFetchRequest(entityName: "TableY")
let predicateTableY = NSPredicate(format: "tableyid == %@", idEleY)
fetchRequestTableY.predicate = predicateTableY
do {
let fetchedEntitiesTableY = try managedObjectContext.executeFetchRequest(fetchRequestTableY) as! [TableY]
if (fetchedEntitiesTableY.count == 0){
let newTableY = NSEntityDescription.insertNewObjectForEntityForName("TableY", inManagedObjectContext: managedObjectContext) as! TableY
newTableY.tableyid = "ccbnnzmz"
newTableY.setValue(NSSet(array: elementsTableX), forKey: "tableX")
do {
try newTableY.managedObjectContext!.save()
print("Saved!")
return newTableY
} catch let error as NSError {
print("Error")
return nil
}
}else{
print("This element exist")
return fetchedEntitiesTableY[0]
}
} catch {
return nil
}
}
func addElementToTableZ(idEleZ: String, elementsTableY: [TableY])->TableZ?{
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequestTableZ = NSFetchRequest(entityName: "TableZ")
let predicateTableZ = NSPredicate(format: "tablezid == %@", idEleZ)
fetchRequestTableZ.predicate = predicateTableZ
do {
let fetchedEntitiesTableZ = try managedObjectContext.executeFetchRequest(fetchRequestTableZ) as! [TableZ]
if (fetchedEntitiesTableZ.count == 0){
let newTableZ = NSEntityDescription.insertNewObjectForEntityForName("TableZ", inManagedObjectContext: managedObjectContext) as! TableZ
newTableZ.tablezid = "ccbnnzmz"
newTableZ.setValue(NSSet(array: elementsTableZ), forKey: "tableZ")
do {
try newTableZ.managedObjectContext!.save()
print("Saved!")
return newTableZ
} catch let error as NSError {
print("Error")
return nil
}
}else{
print("This element exist")
return fetchedEntitiesTableZ[0]
}
} catch {
return nil
}
}
答案 0 :(得分:0)
我建议使用魔法记录库。它将基于键值编码工作,因此我们可以使用此库实现多对多的关系。