I have some NSManaged Objects called Word, who are their own set. A pre-populated list of word objects and their associated properties.
I have a second NSManaged Object called WordSets, who is to contain a partial list of Word Objects. (one to many)
I am trying to understand how to link the WordSets to the pre-existing Word objects.
I have a tableView where I can select a word object. I have an add button, who calls an outlet to the following code...
NSInteger row = [_wordsTableInEditWindow selectedRow];
NSLog(@"Add Selected: %ld", (long) row);
[_wordArrayController setSelectionIndex:row]; // set selected row
NSLog(@"Word? %@", _wordArrayController.selectedObjects);
NSLog, after selecting the second table view item and calling the outlet:
2013-07-23 00:50:26.074 Words Admin[19540:303] Add Selected: 1
2013-07-23 00:50:26.075 Words Admin[19540:303] Word? (
"<Word: 0x1006cc370> (entity: Word; id: 0x10068a830
<x-coredata://912FFFF6-E367-4787-8ECE-C279EC0B94B8/Word/p106> ; data:
{\n audioFile = \"bird.caf\";\n context = \"BBBBird is the word!\";\n
wordSetRel = \"<relationship fault: 0x1006e0850 'wordSetRel'>\";\n})"
)
What I don't really get here is that I only want/need to add a reference to the object.
I have both the table selection, and apparently a skeleton of the object which appears to have been somehow construed into an NSArray, that I cannot seem to access.
Object WordSets has a 1-many with word, and vice versa, but I can't seem to get a handle on the object in order to add the relationship.
I am guessing that I am doing something wrong in KVC land or something. The NSlog has the object id right there, so FWIG the task is to take the data in _wordArrayController.selectedObjects, get a handle on the selected object, and then add that object to the WordSets instance i.e:
[wordsetsObject addWordObject:wordObject];
How is this done???
NSLog
will only show you what is currently fetched from the database. "Relationship fault" means simply that the information about the object of the relationship has not been fetched yet. It will be - conveniently - when it is needed.
If you have a reference to your objects, and your relationships are set up correctly, you can simply establish the relationship in the usual way.
word.wordSet = set;
or, more complicated, using the Core Data generated accessor methods:
[wordSet addWordObject:word];
You then save and check if it worked with
NSLog(@"The wordSet of the word “%@“ is “%@“.",
word.name, word.wordSet.name);
To get the reference, just use the only item of the selected list:
Word *word = _selectedItems.count ? _selectedItems[0] : nil;