核心数据加入 Table 多对多关系 - 对象创建

Core Data Join Table Many to Many Relationship - Object Creation

我想为 2 个实体创建连接 table (BadgeProfile):HubProfile 和 Badge。见下图

问题 1: 我想知道如何创建一个 "BadgeProfile" 对象。此对象应映射到一个 "HubProfile" 和一个 "Badge" 对象

问题 2: 创建 "BadgeProfile" 对象后,"HubProfile" 和 "Badge" 稍后都可以访问该对象。 (示例:HubProfile.BadgeProfile)我该怎么做?

以下是实体的 类:

中心资料

class HubProfile: NSManagedObject {
  @NSManaged var id: NSNumber?
  @NSManaged var name: String?
  @NSManaged var hubBadgeProfiles: NSOrderedSet?
}

徽章

class Badge: NSManagedObject {
  @NSManaged var id: NSNumber?
  @NSManaged var name: String?
  @NSManaged var score: NSNumber?
  @NSManaged var badgeProfiles: NSOrderedSet?
}

BadgeProfile

class BadgeProfile: NSManagedObject {
  @NSManaged var id: NSNumber?
  @NSManaged var badge: Badge?
  @NSManaged var hubProfile: HubProfile?
}

P.S: 我知道我不需要创建连接 Table 并且可以使用 Many以 HubProfile <<-- -->> Badge 之间的 Many 关系。但是我想创建连接 table 因为它可以很容易地与后端数据库交互。

创建连接 table 对象:

以与创建任何其他实体相同的方式创建它,例如通过

NSEntityDescription.insertNewObjectForEntityForName(...)

然后只需添加您想要的对象即可link。

badgeProfile.hubProfile = hubProfile
badgeProfile.badge = badge

我建议不要使用有序集。它们很复杂且容易出错。例如,上面的内容确实令人头疼。相反,在 BadgeProfile 实体中使用数字属性来自己跟踪订单。

访问加入的对象

没什么特别的。获取个人资料的所有徽章:

profile.hubBadgeProfiles.map { [=12=].badge! }

(我认为您的属性名称选择不当。我建议 badges,另一边的等价物,profiles 用于 Badge 实体。)

获得特定徽章:

profile.badges.filter { [=13=].badge!.name == "Gold" }.first as? Badge

使用模拟模式向另一个方向前进。

我想我找到了解决办法:

创建示例对象:

func loadBadgeProfilesPart2() //Second Way to Create BadgeProfile
{

    let hubProfileEntity = NSEntityDescription.entityForName("HubProfile", inManagedObjectContext: managedContext)
    let hubProfile1 = HubProfile(entity: hubProfileEntity!, insertIntoManagedObjectContext: managedContext)
    hubProfile1.id = 5
    hubProfile1.name = "EFG"

    let badgeEntity = NSEntityDescription.entityForName("Badge", inManagedObjectContext: managedContext)
    let badge1 = Badge(entity: badgeEntity!, insertIntoManagedObjectContext: managedContext)
    badge1.id = 5
    badge1.name = "Polite"

    let badgeProfileEntity = NSEntityDescription.entityForName("BadgeProfile", inManagedObjectContext: managedContext)
    let badgeProfile1 = BadgeProfile(entity: badgeProfileEntity!, insertIntoManagedObjectContext: managedContext)
    badgeProfile1.id = 3

    let badgeProfile2 = BadgeProfile(entity: badgeProfileEntity!, insertIntoManagedObjectContext: managedContext)
    badgeProfile2.id = 4

    let badgeProfileSet: NSMutableOrderedSet = []  //Add to an NSOrderedSet
    badgeProfileSet.addObject(badgeProfile1)
    badgeProfileSet.addObject(badgeProfile2)

    hubProfile1.hubBadgeProfiles = badgeProfileSet //Assign BadgeProfile to HubProfile
    badge1.badgeProfiles = badgeProfileSet //Assign BadgeProfile to Badge

    do
    {
        try! self.managedContext.save()
    }

}

正在获取 TableView 中的对象

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! BadgeProfileViewCell!
    configureCell(cell, indexPath: indexPath)
    return cell
}

func configureCell(cell: BadgeProfileViewCell, indexPath: NSIndexPath) //Showing in TableView
{
    let badgeProfile = fetchedResultsController.objectAtIndexPath(indexPath) as! BadgeProfile
    cell.nameLabel?.text = badgeProfile.hubProfile?.name
    cell.badgeLabel?.text = badgeProfile.badge?.name
}

这似乎有效。