I'm 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 a Child:
@class Properties;
@interface Units : Properties
@property (nonatomic, retain) NSString * unitDescr;
@property (nonatomic) int16_t unitNumber;
@property (nonatomic, retain) Properties *property;
When I fetch the Parent Properties with this method to display the parent Properties objects in a tableview:
- (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 run in to a problem where the core data context fetches the 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 properties tableview should display just 1 row but it is displaying 4 rows (1 parent and 3 childs).
How do I just return the parent objects?
Thanks.
Take a look at NSFetchRequest
's setIncludesSubentities
method. If your data model reflects your inheritance pattern in your code, then your fetch request won't fetch children entities if you've set it properly.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.includesSubentities = NO;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With