如果多個實(shí)體之間有關(guān)聯(lián),比如Student擁有多本書(Book),怎么像數(shù)據(jù)庫一樣的能夠表示這種關(guān)系?
Core Data提供了relationship來表示實(shí)體(Entity)之間的這種關(guān)系,包括一對一、一對多等。
1 .打開Core Data的模型文件,可以看到每個Entity都有一個Relationships可以設(shè)置。我們在Student里面添加一個books屬性,并將它的類型(Type)設(shè)置為To Many(一對多)。 
2 .給Books添加一個owner屬性,并將Inverse設(shè)為books。這樣的話,只要將book對象添加到Student的books中,就會自動將owner屬性指向該Student對象。通過改變實(shí)體的展示樣式能夠讓我們看的更加清楚。

3 .通過“Editor > NSManagedObject Subclass...”創(chuàng)建兩個實(shí)體所對應(yīng)的類。
Book:
@interface Book : NSManagedObject @property (nonatomic, retain) NSString * title; @property (nonatomic) float price; @property (nonatomic, retain) Student *owner; @end
Student:
@interface Student : NSManagedObject @property (nonatomic, retain) NSString * name; @property (nonatomic) int32_t age; @property (nonatomic, retain) NSOrderedSet *books; @end @interface Student (CoreDataGeneratedAccessors) //沒有實(shí)現(xiàn)- (void)insertObject:(Book *)value inBooksAtIndex:(NSUInteger)idx; - (void)removeObjectFromBooksAtIndex:(NSUInteger)idx; - (void)insertBooks:(NSArray *)value atIndexes:(NSIndexSet *)indexes; - (void)removeBooksAtIndexes:(NSIndexSet *)indexes; - (void)replaceObjectInBooksAtIndex:(NSUInteger)idx withObject:(Book *)value; - (void)replaceBooksAtIndexes:(NSIndexSet *)indexes withBooks:(NSArray *)values; - (void)addBooksObject:(Book *)value;- (void)removeBooksObject:(Book *)value; - (void)addBooks:(NSOrderedSet *)values;- (void)removeBooks:(NSOrderedSet *)values; @end
在Student是通過一個NSOrderdSet來表示一對多的關(guān)系的。這里之所以沒有使用數(shù)組是因?yàn)樾枰WC數(shù)據(jù)的唯一性。我們還需要注意的是,在Student類中生成了許多管理Book的方法,但是這些方法都是沒有實(shí)現(xiàn)的。比如我們需要添加一個增加Book的功能,就需要實(shí)現(xiàn)addBooksObject:。
- (void)addBooksObject:(Book *)value {
NSMutableOrderedSet *books = [self.books mutableCopy];
[books addObject:value];
self.books = books;}4 .保存Student對象與Book對象。
NSManagedObjectContext *context = [AppDelegate appDelegate].managedObjectContext;NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];//創(chuàng)建Student對象Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:context];int r = arc4random_uniform(1000);stu.name = [NSString stringWithFormat:@"Zhangsan: %d", r];NSEntityDescription *bEntity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:context];//創(chuàng)建Book對象Book *book = [[Book alloc] initWithEntity:bEntity insertIntoManagedObjectContext:context];book.title = @"紅樓夢";//添加Book對象[stu addBooksObject:book];//保存Student對象[context insertObject:stu];[context save:nil];
5 .查詢Student對象,并通過打印查看是否保存了Book,并且能否通過book.owner得到它與Student對象的關(guān)系。
NSManagedObjectContext *context = [AppDelegate appDelegate].managedObjectContext;NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];NSArray *arr = [context executeFetchRequest:request error:nil];for (Student *stu in arr) {
NSLog(@"Name: %@", stu.name);
for (Book *b in stu.books) {
NSLog(@"Book: %@ -> %@", b.title, b.owner);
}}6 .從結(jié)果可以看到,b.owner確實(shí)指向了一個Student對象。
2015-02-04 09:07:43.391 02-03-CoreDataRelationship[5169:235934] Name: Zhangsan: 333
2015-02-04 09:07:43.394 02-03-CoreDataRelationship[5169:235934] Book: 紅樓夢 -> <Student: 0x7f9720d48bd0> (entity: Student; id: 0xd000000000040000 <x-coredata://C07E5BAC-C3F6-44B6-B21C-C3D3FBFA4ED1/Student/p1> ; data: {
age = 0;
books = (
"0xd000000000040002 <x-coredata://C07E5BAC-C3F6-44B6-B21C-C3D3FBFA4ED1/Book/p1>"
);
name = "Zhangsan: 333";})7 .總的來說Core Data自動替我們管理了實(shí)體(對象)之間的依賴關(guān)系,能夠省去不少代碼。
本文檔由長沙戴維營教育整理。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文名稱:Coredata第二課實(shí)體間的關(guān)系-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://chinadenli.net/article36/ceeipg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、企業(yè)建站、軟件開發(fā)、企業(yè)網(wǎng)站制作、網(wǎng)站排名、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容