I am new to iOS dev and Core Data. I have a parent NSManagedObject
@class Units; @interface Properties : NSManagedObject @property (nonatomic, retain) NSString * descr; @property (nonatomic, retain) NSString * address; @property (nonatomic, retain) NSString * city; @property (nonatomic, retain) NSString * state; @property (nonatomic, retain) NSString *zipCode; @property (nonatomic, retain) NSString * imageKey; @property (nonatomic, retain) NSString * numberOfUnit; @property (nonatomic, retain) NSData * thumbnailData; @property (nonatomic, strong) UIImage * thumbnail; @property (nonatomic) double orderingValue; @property (nonatomic, retain) NSSet *units;
And the baby:
@class Properties; @interface Units : Properties @property (nonatomic, retain) NSString * unitDescr; @property (nonatomic) int16_t unitNumber; @property (nonatomic, retain) Properties *property;
When I get the parent properties using this method to display the parent property objects in the table view:
- (void)loadAllItems { if (!allItems) { NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *e = [[model entitiesByName] objectForKey:@"Properties"]; [request setEntity:e]; NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"orderingValue" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sd]]; NSError *error; NSArray *result = [context executeFetchRequest:request error:&error]; if (!result) { [NSException raise:@"Fetch failed" format:@"Reason: %@", [error localizedDescription]]; } allItems = [[NSMutableArray alloc] initWithArray:result]; } }
I ran into a problem when the main data context retrieves child objects of the parent entity. I just want to return the parent objects.
For example, if I have a property with 3 units, the tableview property should only display 1 row, but it displays 4 rows (1 parent and 3 children).
How do I return parent objects?
Thanks.
source share