简体   繁体   中英

Core Data Table Relationship and Fetchrequest

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 AI how can i fetch the text from B based on this criteria.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM