0

I'm having issues in getting Core Data info from 3 linked entities. Some of it works, while some doesn't.

Model
Entity        Accounts                RegDate
relationship   heldby  >> to-many >>  inAccounts
attributes     balance                 addDate
               name

Entity        RegDate                 Regster
relationship  regheldBy >> to-many >> inRegDate
attributes    addDate                 amount

When I do this:

Regster *regster = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = regster.amount;
NSLog(@"Regster: %@  %@", regster.amount, regster.inRegDate.addDate);

regster.amount displays the expected value, but regster.inRegDate.addDate null.

When I do this:

RegDate *regdate = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = regdate.addDate;
NSLog(@"RegDate: %@", regdate.addDate);

regdate.addDate displays the expected value, whereas previous example I get null. Additionally, I cannot get amount to display. I thought I could use: regdate.regheldBy.amount > but this give error "Property 'amount' not found on object of type 'NSSet'"

Not sure what I'm doing wrong... I'm guessing I might have the relationships setup improperly. I've tried many different things, but can't get it to work.

Any ideas?

BlizzofOZ
  • 115
  • 1
  • 2
  • 13
  • I don't know about the relationships but the last problem is easy enough. Since `regheldBy` can point to many Regster objects, it has no idea which one you want to get the `amount` value from. You need to pick a specific one out of the set it references. – Phillip Mills May 25 '12 at 12:14
  • I hear what you are saying and I understand... just not sure how that is accomplished. In all of the examples I've seen, I don't see anything different in coding. Can you expand on this? I'll try to research and see how this is done. – BlizzofOZ May 25 '12 at 13:46
  • If you have one RegDate that is related to two Regster objects, how would you identify which one's amount you wanted to display? (I'm not asking about code, just the "business requirement" you'd need to apply.) – Phillip Mills May 25 '12 at 14:11

1 Answers1

1

It might be caused by not setting inverse relationships for all your relationships.

Also because regdate.regheldBy is a to-many relationship, it is represented by a set containing Regster objects. You can't call amount on the set, you first have to select a Regster object from your set.

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • Seems like @mprivat and Phillip Mills are saying the same thing. I'm not sure how to select a particular Regster object from a set. Can you expand on this? I'll try to see what I can dig up. – BlizzofOZ May 25 '12 at 13:48
  • Do I need to setup a array, pulling in redate.regheldBy using allObjects? – BlizzofOZ May 25 '12 at 16:18
  • No, you can just do something like: `for(Regster *r in regdate.regheldBy) { NSLog(@"%f", r.amount); }` – mprivat May 25 '12 at 16:26
  • I understand this code and what it is doing. Unfortunately, it is not displaying the NSLog, which means it is not entering the for loop. – BlizzofOZ May 25 '12 at 17:14
  • which means your set is empty. You can check that with NSLog(@"%d", [regdate.regheldBy count]); – mprivat May 25 '12 at 17:39
  • So this tells me there is no data in the Regster entity. But this is untrue as I can display the amount as I stated above. – BlizzofOZ May 25 '12 at 18:07
  • No, this only tells you that there is no `Regster` tied to that `RegDate` – mprivat May 25 '12 at 18:08
  • Right, right, understood. So do I possibly have the model/relationships setup wrong? – BlizzofOZ May 25 '12 at 18:11
  • Well, it could be many things frankly. It could be as simple as forgetting to add the Regster to the RegDate NSManagedObject. Your best bet would be to open up the sqlite file and see if there is a link in your data between the two records you are interested in. – mprivat May 25 '12 at 18:14
  • While these answers haven't fixed my problem, @mprivat has shown me that I'm not saving anything. After in-depth review, I realized my flaw in understanding some of the principles of Core Data AND the example I was basing this off of. I understand this and am working and researching on how to make this work. – BlizzofOZ Jun 05 '12 at 17:28