我有一个示例应用程序,我正在通过一些Swift&核心数据在将代码移动到真实应用程序之前工作。我需要一个多对多的关系。下面的示例显示了使用在Xcode中创建新应用程序并选择Core Data时使用默认锅炉编码的一对多功能。
import UIKit
import CoreData
class ViewController: UIViewController {
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var teamInformation:TeamInformation?
var playerInformation:PlayerInformation?
var position:Position?
var rosterInformation:RosterInformation?
var positionsSet = NSMutableSet()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadData()
}
func loadData() {
//Team Information
var newTeamInformation1 = NSEntityDescription.insertNewObjectForEntityForName("TeamInformation", inManagedObjectContext: managedObjectContext!) as TeamInformation
newTeamInformation1.teamName = "Team 1"
var newTeamInformation2 = NSEntityDescription.insertNewObjectForEntityForName("TeamInformation", inManagedObjectContext: managedObjectContext!) as TeamInformation
newTeamInformation2.teamName = "Team 2"
var newTeamInformation3 = NSEntityDescription.insertNewObjectForEntityForName("TeamInformation", inManagedObjectContext: managedObjectContext!) as TeamInformation
newTeamInformation3.teamName = "Team 3"
var newTeamInformation4 = NSEntityDescription.insertNewObjectForEntityForName("TeamInformation", inManagedObjectContext: managedObjectContext!) as TeamInformation
newTeamInformation4.teamName = "Team 4"
var newTeamInformation5 = NSEntityDescription.insertNewObjectForEntityForName("TeamInformation", inManagedObjectContext: managedObjectContext!) as TeamInformation
newTeamInformation5.teamName = "Team 5"
//Save Data
appDelegate.saveContext()
//Player Information
var newPlayerInformation1 = NSEntityDescription.insertNewObjectForEntityForName("PlayerInformation", inManagedObjectContext: managedObjectContext!) as PlayerInformation
newPlayerInformation1.firstName = "Player 1"
var newPlayerInformation2 = NSEntityDescription.insertNewObjectForEntityForName("PlayerInformation", inManagedObjectContext: managedObjectContext!) as PlayerInformation
newPlayerInformation2.firstName = "Player 2"
var newPlayerInformation3 = NSEntityDescription.insertNewObjectForEntityForName("PlayerInformation", inManagedObjectContext: managedObjectContext!) as PlayerInformation
newPlayerInformation3.firstName = "Player 3"
var newPlayerInformation4 = NSEntityDescription.insertNewObjectForEntityForName("PlayerInformation", inManagedObjectContext: managedObjectContext!) as PlayerInformation
newPlayerInformation4.firstName = "Player 4"
var newPlayerInformation5 = NSEntityDescription.insertNewObjectForEntityForName("PlayerInformation", inManagedObjectContext: managedObjectContext!) as PlayerInformation
newPlayerInformation5.firstName = "Player 5"
//Save Data
appDelegate.saveContext()
//Positions
var newposition1 = NSEntityDescription.insertNewObjectForEntityForName("Position", inManagedObjectContext: managedObjectContext!) as Position
newposition1.position = "C"
var newposition2 = NSEntityDescription.insertNewObjectForEntityForName("Position", inManagedObjectContext: managedObjectContext!) as Position
newposition2.position = "LW"
var newposition3 = NSEntityDescription.insertNewObjectForEntityForName("Position", inManagedObjectContext: managedObjectContext!) as Position
newposition3.position = "RW"
var newposition4 = NSEntityDescription.insertNewObjectForEntityForName("Position", inManagedObjectContext: managedObjectContext!) as Position
newposition4.position = "G"
var newposition5 = NSEntityDescription.insertNewObjectForEntityForName("Position", inManagedObjectContext: managedObjectContext!) as Position
newposition5.position = "D"
//Save Data
appDelegate.saveContext()
//Add players to Team 1 Roster
var newPlayerToAddToRoster1 = NSEntityDescription.insertNewObjectForEntityForName("RosterInformation", inManagedObjectContext: managedObjectContext!) as RosterInformation
newPlayerToAddToRoster1.rosterInformationToPlayerInformationRelationship = newPlayerInformation1
newPlayerToAddToRoster1.rosterInformationToTeamInformationRelationship = newTeamInformation1
newPlayerToAddToRoster1.rosterInformationToPositionRelationship = newposition4
newPlayerToAddToRoster1.playerNumber = "29"
//Save Data
appDelegate.saveContext()
var newPlayerToAddToRoster2 = NSEntityDescription.insertNewObjectForEntityForName("RosterInformation", inManagedObjectContext: managedObjectContext!) as RosterInformation
newPlayerToAddToRoster2.rosterInformationToPlayerInformationRelationship = newPlayerInformation2
newPlayerToAddToRoster2.rosterInformationToTeamInformationRelationship = newTeamInformation1
newPlayerToAddToRoster2.rosterInformationToPositionRelationship = newposition1
newPlayerToAddToRoster2.playerNumber = "5"
//Save Data
appDelegate.saveContext()
}
}
当我检查SQLite3中的表时,一切都按预期工作,并且关系(ZROSTERINFORMATION)正在运行。因此,下一步是将位置实体移动到多个。名单中的一个游戏可以有很多位置,而名册上有很多玩家。
sqlite> select * from ZPLAYERINFORMATION;
1|1|1|Player 4|
2|1|1|Player 3|
3|1|2|Player 2|
4|1|2|Player 1|
5|1|1|Player 5|
sqlite> select * from ZPOSITION;
1|2|2|C
2|2|1|D
3|2|1|LW
4|2|1|RW
5|2|2|G
sqlite> select * from ZROSTERINFORMATION;
1|3|1|4|5|3|29
2|3|1|3|1|3|5
sqlite> select * from ZTEAMINFORMATION;
1|4|1|Team 3
2|4|1|Team 4
3|4|3|Team 1
4|4|1|Team 5
5|4|1|Team 2
当我将rosterInformationToPositionRelationship更改为Many-To-Many时,它会将关系更改为NSSet。
我认为通过以下操作可以满足NSset:
//Add players to Team 1 Roster
var newPlayerToAddToRoster1 = NSEntityDescription.insertNewObjectForEntityForName("RosterInformation", inManagedObjectContext: managedObjectContext!) as RosterInformation
newPlayerToAddToRoster1.rosterInformationToPlayerInformationRelationship = newPlayerInformation1
newPlayerToAddToRoster1.rosterInformationToTeamInformationRelationship = newTeamInformation1
//Add multiple positions...
newPlayerToAddToRoster1.rosterInformationToPositionRelationship.setByAddingObject(newposition4)
newPlayerToAddToRoster1.rosterInformationToPositionRelationship.setByAddingObject(newposition5)
//Add player number
newPlayerToAddToRoster1.playerNumber = "29"
但是通过查看SQLite3表,但添加的玩家应该有2个位置(" G"&" D")所以它似乎只是采取了最后一个条目。
sqlite> select * from ZROSTERINFORMATION;
1|3|1|2|2|29
2|3|1|3|2|5
sqlite> select * from ZPLAYERINFORMATION;
1|1|1|Player 3|
2|1|2|Player 1|
3|1|2|Player 2|
4|1|1|Player 4|
5|1|1|Player 5|
sqlite> select * from ZPOSITION;
1|2|1|C
2|2|1|D
3|2|1|LW
4|2|1|RW
5|2|1|G
所以我尝试了这个:
//Add players to Team 1 Roster
var newPlayerToAddToRoster1 = NSEntityDescription.insertNewObjectForEntityForName("RosterInformation", inManagedObjectContext: managedObjectContext!) as RosterInformation
newPlayerToAddToRoster1.rosterInformationToPlayerInformationRelationship = newPlayerInformation1
newPlayerToAddToRoster1.rosterInformationToTeamInformationRelationship = newTeamInformation1
//Add multiple positions...
positionsSet.setByAddingObject(newposition4)
positionsSet.setByAddingObject(newposition5)
newPlayerToAddToRoster1.rosterInformationToPositionRelationship.setByAddingObject(positionsSet)
//Add player number
newPlayerToAddToRoster1.playerNumber = "29"
当我检查SQLite3时,我可以看到它再一次只显示了positionSet NSMutableSet中的最后一个对象...
我做错了什么?
sqlite> select * from ZPLAYERINFORMATION;
1|1|1|Player 5|
2|1|2|Player 1|
3|1|1|Player 3|
4|1|2|Player 2|
5|1|1|Player 4|
sqlite> select * from ZPOSITION;
1|2|1|RW
2|2|1|G
3|2|1|D
4|2|1|C
5|2|1|LW
sqlite> select * from ZTEAMINFORMATION;
1|4|1|Team 5
2|4|3|Team 1
3|4|1|Team 2
4|4|1|Team 3
5|4|1|Team 4
sqlite> select * from ZROSTERINFORMATION;
1|3|1|2|2|29
2|3|1|4|2|5
当我更改为以下代码时:
positionsSet.addObject(newposition4)
positionsSet.addObject(newposition5)
println("positionsSet \(positionsSet.description)");
这是输出:
positionsSet {(
<One_To_Many.Position: 0x7fef8b492370> (entity: Position; id: 0xd000000000100004 <x-coredata://AE608FC7-7683-490E-A6E1-17D4F058D54A/Position/p4> ; data: {
position = G;
positionToRosterInformationRelationship = (
);
}),
<One_To_Many.Position: 0x7fef8b492540> (entity: Position; id: 0xd000000000140004 <x-coredata://AE608FC7-7683-490E-A6E1-17D4F058D54A/Position/p5> ; data: {
position = D;
positionToRosterInformationRelationship = (
);
})
)}
我错过了什么?
class RosterInformation: NSManagedObject {
@NSManaged var playerNumber: String
@NSManaged var rosterInformationToPlayerInformationRelationship: PlayerInformation
@NSManaged var rosterInformationToPositionRelationship: NSSet
@NSManaged var rosterInformationToTeamInformationRelationship: TeamInformation
}
class Position: NSManagedObject {
@NSManaged var position: String
@NSManaged var positionToRosterInformationRelationship: NSSet
}
class TeamInformation: NSManagedObject {
@NSManaged var teamName: String
@NSManaged var teamInformationToRosterInformationRelationship: NSSet
}
class PlayerInformation: NSManagedObject {
@NSManaged var firstName: String
@NSManaged var lastName: String
@NSManaged var playerInformationToRosterInformationRelationship: NSSet
}