Alex Ackerman
Alex Ackerman

Reputation: 23

Swift NSFetchedResultsController Best Practice with Nested Objects

I have a question regarding best practices when using a NSFetchedResultsController and Core Data when you have a Managed Object that has a set of Managed Objects as a property, such as:

class SomeObject: NSManagedObject {
    @NSManaged var name: String
    @NSManaged var notes: String?
    @NSManaged var otherObjects: Set<OtherObject>?
}

class OtherObject: NSManagedObject {
    @NSManaged var name: String
    @NSManaged var notes: String?
    @NSManaged var parent: SomeObject
}

I am using a NSFetchedResultsController to populate a UITableView for the "SomeObject" instances. When a user selects one of the table elements, it goes to another UIView that has the details about the SomeObject instance as well as a UITableView with the list of OtherObjects assigned.

My question is what is the best practice for efficient Table View management? Should I create another NSFetchedResultsController using the parent SomeObject element to filter and query the database or should I just fill the table with the OtherObjects pulled during the initial data query? I am injecting the SomeObject instance into the next UIView so the new view has the ManagedObjectContext and original information.

Upvotes: 0

Views: 211

Answers (1)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

My question is what is the best practice for efficient Table View management?

Premature optimization is the root of all evil (Donald Knuth)

However, Core Data is no relational database management system. In Core Data relationships are references. So use that references. This is the default. (Core Data uses faulting and uniquing, so I do not expect a big difference. But if you get problems with the runtime behavior you can still post a Q on SO.)

Upvotes: 0

Related Questions