user6631314
user6631314

Reputation: 1966

IOS/Objective-C/Core-Data: Does new Core Data object come with relationship objects?

I am importing book records from JSON into NSManagedobjects that have their own attributes but also relationships to other entities, authors and publishers.

For each book item that comes down from the cloud via JSON, I create a new bookFromServer object. I store the attributes from JSON into this object. This is simple enough with title, bookid etc.

However, the JSON also contains other information that properly belongs in other entities, ie publisher publisher name, publisher address, author first name author last name, author date of birth etc.

My question, is whether I also have access to "phantom" or uninstantiated managedobjects of the entitites with which my object has a relationship? Or do I need to create properties in the object file for each and every attribute.

Here is the NSObject file for BookFromServer

#import <CoreData/CoreData.h>
@class Books;
@class Authors;
@class Publishers;


@interface BookFromServer : NSObject
@property (nonatomic, retain) NSNumber * bid; 
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * authorid;
@property (nonatomic, retain) NSNumber * publisherid;
 //this is relationship 
@property (nonatomic, retain) Authors *author;
 //this is relationship
@property (nonatomic, retain) Publishers *publisher;


@end

I would like to store author info in something like author.firstname, not as a separate authorfirstname property in books. So my question is when I instantiate a book object, do I get the use of the attributes in the objects available through the relationships?

Upvotes: 0

Views: 47

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70966

My question, is whether I also have access to "phantom" or uninstantiated managedobjects of the entitites with which my object has a relationship? Or do I need to create properties in the object file for each and every attribute.

It's exactly the same as with any other kind of object. Using Core Data doesn't change anything here.

New objects are only created when your code creates them. Core Data never creates a new object implicitly or automatically. So if your managed object has relationships to other managed objects, you need to create the managed objects and then update the objects so that their relationships refer to each other.

Properties of managed objects are available when the object is created, so a string or numeric property exists as soon as the managed object exists.

Upvotes: 1

Related Questions