【问题标题】:Get related / associated entities from NSSet - CoreData从 NSSet 获取相关/关联实体 - CoreData
【发布时间】:2012-02-19 14:52:55
【问题描述】:

我有 2 个自动生成的实体:

@interface ContactEntity : Entity

@property (nonatomic, retain) NSString *caption;
@property (nonatomic, retain) NSString *image;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *text;
@property (nonatomic, retain) NSSet *pointLink;

@end

@interface PointEntity : Entity

@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;
@property (nonatomic, retain) NSSet *entityLink;
@property (nonatomic, retain) EntityEntity *entityTypeLink;

@end

它们以ManyToMany的形式相互联系,即一个联系人有很多点,一个点里面有很多联系人。

然后我得到第一个实体:

ContactModel *contact = [[[ContactModel alloc] init] autorelease];
// this is FetchRequest, returns array of all entities
self.items = [contact list:contact];  
// i get only one, all is OK here, this entity has related PointEntity in DB
ContactEntity *contactEntity = [self.items objectAtIndex:self.selection]; 

当我尝试在选定的 ContactEntity 中使用 NSSet 获取相关的 PointEntity 时,我总是得到 NULL 或空数组。这两个都不起作用:

NSArray *points = [contactEntity.pointLink allObjects];
PointEntity *pointEntity = [contactEntity.pointLink anyObject];

NSInteger x1 = [points count]; // always 0
id x2 = pointEntity.latitude; // always 0

for (PointEntity *x in contactEntity.pointLink) // isn't enumerated because count = 0
{
    id x3 = x.latitude;
}

感谢任何想法。我错过了什么吗,也许我需要使用 NSPredicate 从 PointEntity 中选择与 ContactEntity 相关的实体?

谢谢。

附:我的问题与此类似,但该建议对我不起作用,我无法使用主实体的 NSSet 加载关联实体:( CoreData: many-to-many relationship

【问题讨论】:

    标签: ios core-data


    【解决方案1】:

    找到答案...我尝试在 CoreData 中创建新记录时使用自动生成实体的属性,同时正确的方法是使用生成的方法,例如 addPointLinkObject、addEntityLinkObject 等

    例如,我有 3 个表:

    Contacts (one person may have many locations) 
    << - >> 
    Points (one location can contain many people) 
    << - > 
    EntityTypes (just a type of a location, in this case type is CONTACT)
    

    xCode 自动生成的实体之一:

    @interface PointEntity : Entity
    
    @property (nonatomic, retain) NSNumber *latitude;
    @property (nonatomic, retain) NSNumber *longitude;
    @property (nonatomic, retain) NSSet *entityLink; // reference to Contacts table (ManyToMany)
    @property (nonatomic, retain) EntityEntity *entityTypeLink; // reference to EntityType table (OneToMany)
    
    @end
    
    @interface PointEntity (CoreDataGeneratedAccessors)
    
    - (void)addEntityLinkObject:(ContactEntity *)value;
    - (void)removeEntityLinkObject:(ContactEntity *)value;
    - (void)addEntityLink:(NSSet *)values;
    - (void)removeEntityLink:(NSSet *)values;
    
    @end
    

    我尝试了以下操作:

    // create 3 new instances - one for each entity
    
    ContactEntity *contactEntity = [model create:model];
    PointEntity *pointEntity = [point create:point];
    EntityModel *entity = [[[EntityModel alloc] init] autorelease];
    entity.name = model.table;
    EntityEntity *entityEntity = [[entity list:entity] objectAtIndex:0];
    
    // then i tried to use entity's properties directly to bind entities
    // it works, but it works only on DB level when we add new records, but somehow something was missed and thus such selection did not work later - [pointEntity allObjects]
    
    //pointEntity.entityTypeLink = entityEntity; // WRONG !!!
    //pointEntity.entityLink = contactEntity.pointLink;
    //contactEntity.pointLink = pointEntity.entityLink;
    
    // then i replaced 3 lines above with these ones
    
    [pointEntity addEntityLinkObject:contactEntity]; // CORRECT !!!
    [contactEntity addPointLinkObject:pointEntity];
    [entityEntity addPointLinkObject:pointEntity];
    
    [context save]; // save changes made with entities in current CoreData context
    
    // now [pointEntity allObjects] and [pointEntity anyObject] work as expected
    

    有用的链接 -

    Coredata and Generated subclass for NSManagedObject with relations

    https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html#//apple_ref/doc/uid/TP40002154

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多