Basic Many-to-Many

I have an example application in which I work on Swift and Core Data before translating the code into a real application. I have a need for many-to-many relationships. The following example shows a one-to-many example that works great using the default boiler code when you create a new application in Xcode and select Core Data.

enter image description here

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()

    }
}

When I browse tables in SQLite3, everything works as expected and the relationships (ZROSTERINFORMATION) function. So, the next step is to move the Position object over many. A game in the registry can have many positions, and in the registry there are many players.

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

When I switch to rosterInformationToPositionRelationship for Many-to-Many, it changes attitude towards NSSet.

enter image description here

, 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, , Set 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

}
+4

All Articles