-3

I am developing one iPad application using storyboard and core data.I have no good idea about core data.I have 2 tables names A and B. Table a have 2 fields with names datacode and price.In table B there are two fields with names itemcode and text.Table A have set limit.

Table A

datacode price

p1 10

m1 17

p0 28

m3 20

w4 12

Table B

itemcode text

p0 car

p1 bus

m2 pen

m1 ball

p0 ban

r1 book

m3 pencil

n1 tv

w4 radio

The values in itemcode in tableB is the data code in the table A + some other value.i need to fetch the text values from the tableB based on the itemcodes which values corresponding to the datacode in the table A.I how can i fetch the text from B based on this criteria.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3522400
  • 21
  • 1
  • 5
  • @chris I am ne in objective C only 2 week experience.I need to do one project.I do not know the core data relation ship.Please help me . – user3522400 Apr 22 '14 at 05:38

2 Answers2

0

You need to use predicate with your table A's datacode to table B's itemcode, like this:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *aEntity = [NSEntityDescription entityForName:@"TableA" inManagedObjectContext:moc];
[fetchRequest setEntity:BEntity];

NSArray* fetchResults = [moc executeFetchRequest:fetchRequest error:nil];

you will get all the objects of TableA. so if you have one - one relationship with TableB, you can directly access the all the attributes like:

TableA *tableAObject = fetchResults[i];
NSString * itemcode = tableAObject.tableBRelation.itemcode
Keshav
  • 2,965
  • 3
  • 25
  • 30
  • I tried this method but app is terminating and shows the error ....unrecognized selector sent to instance 0x8cb09a0 – user3522400 Apr 22 '14 at 06:50
  • no other method i am using here.My relation ship between the tables are one to one is that problem here? – user3522400 Apr 22 '14 at 06:58
  • relation ship between the tables are one to one: so in this case if you fetch the tableA is enough. look the changes in the above code. – Keshav Apr 22 '14 at 07:08
  • i got all null values.where i have problem. In relationship or code? – user3522400 Apr 22 '14 at 07:15
  • I am using restkit coredatamapping to store data in the tables.All data are saved in the table suceessfully – user3522400 Apr 22 '14 at 07:22
  • I am not sure why you are using it....are you getting the values for tableA attributes? – Keshav Apr 22 '14 at 07:24
  • sure the table A have full values – user3522400 Apr 22 '14 at 07:28
  • is need two entities have same attribute names – user3522400 Apr 22 '14 at 07:29
  • no no, it can have different values. when you are same the values to TableB, you should set the TableB object to TableA object and visa-versa. – Keshav Apr 22 '14 at 07:32
  • you have no idea where i have mistake? – user3522400 Apr 22 '14 at 07:38
  • No, I think fetch is fine, I am not sure how you are saving the values. – Keshav Apr 22 '14 at 07:45
  • keshav i tried this http://stackoverflow.com/questions/20014272/fetch-data-from-core-data-multiple-entities method ale. i both cases my relationship gives null values that is the reason i am fail to fetch values. – user3522400 Apr 22 '14 at 08:40
  • I have asked you to show me to code for saving the values. if you have getting null value means first check how you are saving it. is any thing going wrong while saving.... – Keshav Apr 22 '14 at 08:43
  • How code for data saved is related to relationship.In my tables A data code and Table B itemcode have no null values.I checked the data saved in the db by using coredataeditor.I am working as the part of the team so i have no chance to show codes... – user3522400 Apr 22 '14 at 09:13
  • ok, try to fetch the data for your Table B separately and if you can not getting then you can check for relationship... – Keshav Apr 22 '14 at 09:20
  • separately i will get values from each tables no problem...I checked already. – user3522400 Apr 22 '14 at 09:22
  • ok. for relationship you should set your tables to each other like this: [tableA setTableB:tableBOject]; [tableBOject setTableA:tableA]; – Keshav Apr 22 '14 at 09:35
  • i need to set key path for relationship? http://stackoverflow.com/questions/13096182/simple-retrieving-values-from-coredata-returns-null-for-entities – user3522400 Apr 22 '14 at 09:55
  • where's define [fetchRequest setEntity:BEntity]; ? – Gangani Roshan Jul 17 '17 at 04:45
0

First you need to create relationship between your both tables A and B. After that you have to fetch records based on you relations..... You will fetch your records like this....

NSMutableArray *arrObj = [[NSMutableArray alloc]init];
for(TableB *tblObj in [TableAObj relationWithTblB]){
      [arrObj addObject:tblObj];
}
NSLog(@"Your records related with tableA = %@",arrObj);
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28