开发者

Core Data => Adding a related object always nil

I have two tables related: DataEntered and Model

DataEntered -currentModel

Model

One DataEntered can have only ONE Model, but a Model can stay into many DataEntered.

The relationship is from DataEntered to Model (No To Many-relathionship) and no inverse relation.

XCode generates开发者_如何学运维 the setters for DataEnteredModel:

@property (nonatomic, retain) NSSet * current_model;

- (void)addCurrent_modelObject:(CarModel *)value;
- (void)addCurrent_model:(NSSet *)value;

I have a Table and when I select a model, I want to store it to DataEntered:

Model *model = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Model %@",model.name); // ==> gives me the correct model name
[dataEntered addCurrent_modelObject:model]; // ==> always nil
[dataEntered setCurrent_model:[fetchedResultsController objectAtIndexPath:indexPath]]; // the same, always nil

what I'm doing wrong ?????

thanks,

r.

edit:

You're right, first I had a to-many relationship, and XCode generated the entity code, that's why after changed the relathionship it still was set as a SET.

Now I've re-generated the entity, and now I have the property like this:

@property (nonatomic, retain) CarModel * current_car_model;

But I'm still getting the same problem.

Now, I've added the inverse relathionship and re-generated the entities. Both entities hav a TO-One relationship. Still having the same problem.

Ok, another try: The relathionship from model to DataEntered is now To-Many. The entites are regenerated again, and now the model is assigned with:

Model *model = [fetchedResultsController objectAtIndexPath:indexPath];

[dataEntered addCurrent_modelObject:model];

And the very last, both relathions are to-many. The same problem exists.

But what I don't understand is why I have to have an inverse relation in this case.

To resume: how I have to define my relations in this case ?

DataEntered can have only ONE Model.

Each Model can be related into many DataEntered.

In an SQL database definition, a field with the id of the model stored in a simple longint field in the DataEntered would be enough. When the relathion is to-many I can't see the field in the .sqlite database, now that I've changed again into To-One, I can see the current_model field as integer. But again, I have in the Model table a field with the related DataEntered, and I think this is wrong, as I'm not going to store Nothing here.

Any light would be appreciated ...

:-)

thanks .....

r.


If your attribute is created as a set, then you don't have it set as a to-one relationship but a to-many. So this:

@property (nonatomic, retain) NSSet * current_model;

...indicates a to-many relationship.

If your model entity has a relationship to the DataEntered entity but you haven't set the same reciprocal relationship you can expect problem.

Edit:

I think you're major problem is that you're thinking of Core Data as a form of SQL, which it is not. Entities are not tables. Objects are not records. Core Data is an object graph and an object graph is much different than SQL. For one thing, it is possible to have an orphaned object in Core Data but you can't really lose a record in SQL. That is why Core Data really wants reciprocal relationships.

Instead of thinking in SQL terms, you should think of it as if you were creating an object graph from scratch without Core Data. How would you handle it if you just wrote a DataEntered class and a model class yourself from scratch? How would you manually relate them. Looking at it from that perspective makes Core Data easier to understand.

I would offer that your choice of entity names might cause problems. It looks like you model entity is actually named "_model". Apple reserves symbols beginning with an underscore and "model" of course could easily show up internally in Core Data so you might be having a name collision. Try changing the name to something like "CarModel" if you like underscores, change it to "CarModel_".

Also, the "DataEntered" entity/class seems poorly named. "Data Entered" is an action not an object. The entity/class should represent some physical or data object. For example, "CustomerCar" would be a entity/class that models a car which in turn would have a "model" relationship to a "CarModel" entity/class.

It is very, very hard to debug Core Data with just a textual description of the object graph especially, (no offense) when the person describing the graph is a novice. It would help if you could take a screen shot of the data model editor and post that so we can see it.


Well, finally I found the problem.

dataEntered was nil ............

I am not accustomed to assign values to nil objects without an error ...

:-)

And I don't take as an offense, I'm really a novice with Core Data, but not with SQL, but beacuse my approach wasn't working, finally I mess all around ...

thanks and sorry!

regards,

r.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜