Core Data Multi-Table Association

Source: Internet
Author: User

1. Concept Introduction

When there are complex data relationships in coredata, a table is difficult to meet the requirements, and you need to understand the use of CoreData multi-table associations.

The relationship between a curriculum and a chapter table: a course is one-to-many and one-to-one chapters can correspond to a single course; And the relationship between the curriculum and the instructor table: a course can have multiple lecturers, a lecturer can also speak multiple courses, and they are many-to-many relationships.

Tables are linked by this mutually binding relationship.

The following is a code demonstration of the movie and cast tables:

Steps to create:

1> Create the data Model;2> Create Movie,actor table, set foreign key;3> Create Nsmanagedobject subclass; Code implementation:
1 #import "ViewController.h"2 #import<CoreData/CoreData.h>3 #import "Actor.h"4 #import "Movie.h"5 6 7 @interfaceViewcontroller ()8 9 {TenNsmanagedobjectcontext *CTX; One     A } - @end -  the @implementationViewcontroller -  -- (void) Viewdidload { - [Super Viewdidload]; +    -     //1. Path to the application package +     //NSLog (@ "%@", [[NSBundle Mainbundle] resourcepath]); A     atNSString *filepath = [[NSBundle mainbundle] Pathforresource:@"MOVIEINFO.MOMD"Oftype:nil]; -     -     //1. Loading the data Model file -Nsmanagedobjectmodel *managemodel =[[Nsmanagedobjectmodel alloc] Initwithcontentsofurl:[nsurl Fileurlwithpath:filepath]]; -     -     //2. Persistent data store scheduler, specifying how the storage is to be used to operate between the application and the database inNspersistentstorecoordinator *coordinator =[[Nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:managemodel]; -     to     //3. Specify the path to the database +NSString *dbfile = [Nshomedirectory () stringByAppendingPathComponent:@"documents/coredata.db"]; -NSLog (@"%@", dbfile); the     *Nserror *error =Nil; $    Panax Notoginseng     //4. How to add persistent storage (either a database or XML) and open the database -[Coordinator Addpersistentstorewithtype:nssqlitestoretype Configuration:nil Url:[nsurl FileURLWithPath:dbfile] Options:nil error:&ERROR]; the     +     if(Error) { A         theNSLog (@"failed to open database"); +}Else{ -NSLog (@"Open Database succeeded"); $     } $     -     //4. Store data, update data, query the data context -CTX =[[Nsmanagedobjectcontext alloc] init]; the     - [CTX Setpersistentstorecoordinator:coordinator];Wuyi     the     //5. Add Movie Data - [self insertmoviedata]; Wu     -     //6. Query Movie Data About [self querymoviedata]; $     - } -  - //Inserting Data A- (void) insertmoviedata{ +  the     //actor One -Actor *actor1 = [nsentitydescription insertnewobjectforentityforname:@"Action"Inmanagedobjectcontext:ctx]; $Actor1.name =@"Jason Statham"; theActor1.age = @ -; theActor1.height = @1.55; the     the     //actor 2 -Actor *actor2 = [nsentitydescription insertnewobjectforentityforname:@"Action"Inmanagedobjectcontext:ctx]; inActor2.name =@"Tom Cruise"; theActor2.age = @ -; theActor2.height = @1.60; About     the     //Movie One theMovie *m1 = [nsentitydescription insertnewobjectforentityforname:@"Movie"Inmanagedobjectcontext:ctx]; theM1.title =@"Life Express"; +M1.year =[NSDate Date]; -M1.rating = @9.7; theM1.actor =Actor1;Bayi     the     //Movie Two theMovie *m2 = [nsentitydescription insertnewobjectforentityforname:@"Movie"Inmanagedobjectcontext:ctx]; -M2.title =@"Top Gun"; -M2.year =[NSDate Date]; theM2.rating = @9.2; theM2.actor =Actor2; the     the     //movie Three -Movie *m3 = [nsentitydescription insertnewobjectforentityforname:@"Movie"Inmanagedobjectcontext:ctx]; theM3.title =@"Super 7"; theM3.year =[NSDate Date]; theM3.rating = @9.5;94M3.actor =Actor1; the     the     //Add a movie to the context the [CTX INSERTOBJECT:M1];98 [CTX INSERTOBJECT:M2]; About [CTX INSERTOBJECT:M3]; - 101    102     //Save103BOOL issuccess =[CTX Save:nil];104     the     if(issuccess) {106NSLog (@"Insert data Successfully!");107     }108 109 } the 111  the //Querying Data113- (void) querymoviedata{ the     the     //query Stensson starring movie theNsfetchrequest *request = [Nsfetchrequest fetchrequestwithentityname:@"Movie"];117    118     //Set Filter Criteria119Request.predicate = [Nspredicate predicatewithformat:@"actor.name = ' Jason Statham '"]; -    121     //Enquiry122Nsarray *result =[CTX executefetchrequest:request error:nil];123      for(Movie *movieinchresult) {124         theNSLog (@"movie title:%@ Movie Rating:%.2lf", movie.title,[movie.rating Doublevalue]);126     }127  - 129  the}

Core Data Multi-Table Association

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.