Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Core data 核心数据多对多关系_Core Data_Swift_Ios8_Many To Many - Fatal编程技术网

Core data 核心数据多对多关系

Core data 核心数据多对多关系,core-data,swift,ios8,many-to-many,Core Data,Swift,Ios8,Many To Many,我有一个示例应用程序,在将代码转移到实际应用程序之前,我正在进行一些Swift和核心数据工作。我需要一种多对多的关系。下面的示例显示了在Xcode中创建新应用程序并选择核心数据时,使用默认锅炉编码的一对多模式运行良好 import UIKit import CoreData class ViewController: UIViewController { let managedObjectContext = (UIApplication.sharedApplication().

我有一个示例应用程序,在将代码转移到实际应用程序之前,我正在进行一些Swift和核心数据工作。我需要一种多对多的关系。下面的示例显示了在Xcode中创建新应用程序并选择核心数据时,使用默认锅炉编码的一对多模式运行良好

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更改为多对多时,它会将关系更改为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

}