Anonymous
Anonymous

Reputation: 1968

Get related / associated entities from NSSet - CoreData

I have 2 autogenerated entities :

@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

They are linked between each other as ManyToMany, i.e. one contact has many points, one point has many contacts inside.

Then a i get first entity :

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

And when i try to get related PointEntity using NSSet in selected ContactEntity i always get NULL or empty array. Neither of this works :

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

Any thoughts are appreciated. Did i miss something, maybe i need to use NSPredicate to select entities from PointEntity that are related to ContactEntity?

Thanks.

P.S. My question is similar to this but that suggestion does not work for me, i cannot get loaded associated entities using NSSet of main entity :( CoreData: many-to-many relationship

Upvotes: 0

Views: 1781

Answers (1)

Anonymous
Anonymous

Reputation: 1968

Answer is found ... i tried to use property of the autogenerated entities when created new records in CoreData, in the meantime the correct way is to use generated methods like - addPointLinkObject, addEntityLinkObject, etc

Example, i have 3 tables :

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)

One of the entities autogenerated by 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

I tried to do the following :

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

Useful links -

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

Upvotes: 0

Related Questions