swiftcore-datamany-to-manynssetnscountedset

swift "Duplicate links in many to many relationship" it is real?


i have many to many relationship in my Core Data model, and i need to add some duplicate links in my links containers, i know the type of link container is NSSet @NSManaged public var linkContainer: NSSet? can i change this type to Array of my links? When i change type to @NSManaged public var linkContainer: Array<Link>? i have this error Unacceptable type of value for to-many relationship: property = "linkContainer"... it is real to add duplicate links in this containers? or can i add some count of used links in NSSet? I also tried NSCountedSet but nothing is working

@NSManaged public var linkContainer: NSCountedSet?

public func addTo(link: link) {
    self.linkContainer?.add(link)
}

and then call by linkContainer.count(for: link)


Solution

  • Yes it is real - relationship are sets, not arrays. It is a 'limitation' of core-data. You should take it as a sign that you are thinking about the problem wrong.

    Whats does an object having a relationship to another object multiple times mean in the context of your app? Would it make sense to express that state as a separate entity, or to model your data differently? It is hard to give guidance without particular information about the system you are trying to model.

    For example: if you had a model that had Customer <<->> Product where that relationship represented that the customer ordered a product. But then you realize that a customer can order a product multiple times you would change your model to be Customer <->> Order <<--> Product which would more accurately reflect the situation you are trying to model.