2012-02-19 17 views
0

はI 2自動生成するエンティティを持っている:すなわち、一方の接点が多くの点を有する、一点は、内部多くの接点を有するNSSetから/に関連する関連エンティティ取得 - CoreData

@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 

これらは多対多のように互いの間で連結されています。

は、私は最初のエンティティを取得:

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; 
} 

いずれの考えもあります。私は、ContactEntityに関連するPointEntityからエンティティを選択するためにNSPredicateを使用する必要があるかもしれません。

ありがとうございました。

P.S.私の質問はこれに似ているが、その提案が私のために動作しません、私は CoreData: many-to-many relationship

答えて

0

回答が発見された主なエンティティ:(のNSSetを使用して、関連するエンティティをロードされることができない...私は、自動生成されたエンティティ際のプロパティを使用しようとしましたXCodeので自動生成するエンティティの

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) 

ワン: - CoreDataで新しいレコードを作成し、その間に正しい方法は次のように生成されたメソッドを使用することですaddPointLinkObject、addEntityLinkObjectなど

例、私は3つのテーブルを持っています:

-

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

// 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 

便利なリンク:

@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 

は、私は次の操作を行うことを試みました

関連する問題