简体   繁体   English

在核心数据中添加对象

[英]Adding objects in Core Data

I am trying to get familiar with Core Data and had Xcode create subclasses of NSManagedObject for me. 我试图熟悉Core Data,并让Xcode为我创建NSManagedObject的子类。 One is AddressAnnotation that just has some information about the location like street, zip, address, etc. The other is: 一个是AddressAnnotation,它仅包含有关位置的一些信息,例如街道,邮政编码,地址等。另一个是:

Map : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *locations;
- (void)addLocationsObject:(AddressAnnotation *)value;
- (void)removeLocationsObject:(AddressAnnotation *)value;
- (void)addLocations:(NSSet *)values;
- (void)removeLocations:(NSSet *)values;

When I want to add a location to the map, I do: 当我想在地图上添加位置时,请执行以下操作:

- (void)insertLocationIntoMap:(Map *)map {  
    [map addLocationsObject:self.address];

I NSLogged the Map object and the address object and both have values in it. 我NSLogged的Map对象和地址对象,都具有值。 When I try adding the object to an existing Map object, it does not seem to get added. 当我尝试将对象添加到现有Map对象时,似乎没有被添加。 The output of that map logged is: 记录该映射的输出为:

<Map: 0x7087480> (entity: Map; id: 0x7084ff0 <x-coredata://8D6D7849-E7EC-48A3-BA95-C082D09E5D6D/Map/p1> ; data: {
    locations = "<relationship fault: 0x768e4e0 'locations'>";
    name = asdf;
})

If I create a new map, and then call insertLocationIntoMap:, the AddressAnnotation object does get added. 如果我创建一个新地图,然后调用insertLocationIntoMap:,则不会添加AddressAnnotation对象。 That map logged is: 记录的地图是:

<Map: 0x767dee0> (entity: Map; id: 0x767d2d0 <x-coredata://8D6D7849-E7EC-48A3-BA95-C082D09E5D6D/Map/p5> ; data: {
    locations =     (
    );
    name = asdfasdf;
})

Am I doing something wrong in adding an AddressAnnotation to the Maps object? 在将AddressAnnotation添加到Maps对象时,我做错什么了吗? This was working when I didn't use subclasses of NSManagedObject and was just using setValue:forKey: everywhere, but I thought this would be more intuitive after I got used to it. 当我不使用NSManagedObject的子类并且仅在任何地方都使用setValue:forKey:时,这是可行的,但是我认为在我习惯之后,这将更加直观。 Thanks! 谢谢!

The first log shows a fault for the relationship. 第一个日志显示该关系的故障。 This tells nothing about which objects are in that relationship. 这并不能说明该关系中存在哪些对象。 You should also log your AddressAnnotation.map relationship. 您还应该记录您的AddressAnnotation.map关系。 Is it a one-to-many or a many-to-many relationship? 是一对多还是多对多关系?

And ultimately, if you want to print out which objects are related, you should use a fetch request to fetch the objects in the locations relationship ( @"self.map == %@" for the AddressAnnotation entity with %@ substituted with the Map instance you are interested in). 最后,如果要打印出哪些对象相关,则应使用获取请求以位置关系获取对象( @"self.map == %@"AddressAnnotation实体,其中%@替换为Map实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM