Peter Kazazes
Peter Kazazes

Reputation: 3628

NSManagedObject Collection

I'm in the process of learning how to work with Core Data.

foo objects are a collection of bar objects that share similar properties. foo has some of its own members, one of which is an array of bar objects.

In order to manage the foos with Core Data, do I need to model only the foos? Do they both need to be subclasses of NSManagedObject? I do not need undo functionality on either.

Upvotes: 1

Views: 87

Answers (2)

Ravi Sisodia
Ravi Sisodia

Reputation: 776

CoreData is just like working with Relational Model in Databases.

Define One table/entity/object(in objective-c, defined in tabular form), and use to related it with another using foreign key mechanism, or for more complex problems use third entity(called relation) i.e also a table.

Table 1: for foo's
    fooID
    barID
    barID
    .
    .
    barID
    fooAttrib1
    .
    .
    fooAttribn
    barArrayAttribID

Table 2: for bar's
    barID
    barAttrib1
    .
    .
    barAttribn

Table 3: for foo's Attribute/member(that is an array pf bar's)
    barArrayAttribID
    barID

in table 3 barArrayAttribID is mapped from table 1, and linked with as many barID, in table 2, as the array have.

Upvotes: 0

bilobatum
bilobatum

Reputation: 8918

I envision your object graph as follows:

Foo entity

  • attribute1
  • attribute2
  • bars (one-to-many relationship)

Bar entity

  • attributeA
  • attributeB
  • displayOrder (another attribute, an int that models the Bar's index in a hypothetical array)
  • foo (one-to-one relationship)

All entities are NSManagedObjects (or a subclass).

Upvotes: 1

Related Questions