[英]Get related / associated entities from NSSet - CoreData
我有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,即一個聯系人有很多點,一個點內部有很多聯系人。
然后ai得到第一個實體:
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相關的實體?
謝謝。
PS我的問題與此類似,但該建議對我不起作用,我無法使用主要實體的NSSet加載關聯的實體:( CoreData:多對多關系
找到答案了...我在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
有用的鏈接-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.