核心数据表关系和获取请求(Core Data Table Relationship and Fetchrequest)

IPhone IT屋
问 题

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.

解决方案

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

本文地址:IT屋 » Core Data Table Relationship and Fetchrequest

问 题

我正在使用storyboard和核心数据开发一个iPad应用程序。我对核心数据不了解。我有两个表名称A和B.表a有2个字段,名称为datacode和price.In表B有两个字段名称为itemcode和text.Table A已设置限制。



表A



数据码价格



p1 10



m1 17



p0 28



m3 20



w4 12



表B



itemcode text



p0 car



p1 bus



m2 pen



m1球



p0 ban



r1书



m3铅笔



n1 tv



w4 radio



tableB中的itemcode中的值是表A中的数据代码+一些其他value.i需要从表B中获取文本值基于项目代码对应于表AI中的数据代码的值,如何基于该标准从B获取文本。


解决方案

您需要将表A的数据代码用于表B的项目代码,如下所示:



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

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

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


您将获得TableA的所有对象。所以如果你与TableB有一对一的关系,你可以直接访问所有的属性,如:



  TableA * tableAObject = fetchResults [一世]; 
NSString * itemcode = tableAObject.tableBRelation.itemcode

本文地址:IT屋 » 核心数据表关系和获取请求