views:

117

answers:

2

I am trying to model an n-to-n relationship in Objective-C. Suppose I have two entities: Movie and Theater. A Movie has an array of Theaters and a Theater has an array of Movies. How do I do this in Objective-C to 1) get the relationship correct and 2) make sure memory is managed correctly.

+2  A: 

On Apple platforms you have access to Core Data, a very nice persistence framework.

IlDan
Yup. Unless you have a very good reason not to, you should use Core Data. Especially since now it works with iPhone now.
Matthew Schinckel
A: 

You can use SQLLitePersistentObjects:

It allows you to define code like the following:

#import "SQLLitePersistentObjects.h"

@interface CFCategory : SQLLitePersistentObject {
  NSString *name;
  CFRegion *region; // where region is another subclass of SQLLitePersistentObject
}

@property(nonatomic, retain, readwrite) NSString *name;
@property(nonatomic, retain, readwrite) CFRegion *region;

@end

And use it in your code:

CFRegion *region = [CFCategory findByRegion:[myRegionObject pk]];

Memory and persistence is automatically handled by the framework. However, if you are working with large data sets be sure to use NSArray objects with the paired arrays functionality instead of allocating and deallocating hundreds or thousands of SQLLitePersistentObjects.

samuraisam