我是 iOS 开发和核心数据的新手。我有一个父 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;
还有一个孩子:
@class Properties;
@interface Units : Properties
@property (nonatomic, retain) NSString * unitDescr;
@property (nonatomic) int16_t unitNumber;
@property (nonatomic, retain) Properties *property;
当我使用此方法获取父属性以在表格视图中显示父属性对象时:
- (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];
}
}
我遇到了核心数据上下文获取父实体的子对象的问题。我只想返回父对象。
例如,如果我有一个具有 3 个单位的属性,则属性表视图应该只显示 1 行,但它显示 4 行(1 个父级和 3 个子级)。
我如何只返回父对象?
谢谢。