2011-12-22 13 views
5

Totalmente nuovo con Core Data, sto creando il mio modello di dati. Ho 33 entità e poche durissime relazioni tra di esse ma molte relazioni a chiave esterna.Come rappresentare le relazioni di chiave esterna in Dati principali - Modello dati in XCode

Come posso gestire le relazioni che non sono esattamente 1-molti o 1-1 o molti-molti ma sono chiavi esterne nel modello di dati di base?

Ad esempio, ho un'entità Contatto, che ha una relazione con una contact_x_mail, e allo stesso tempo contact_x_mail ha una relazione con Mail, che contiene tutte le email. Queste relazioni sono 1-molti o molti-molti. Ma ci sono altri come Istituzione (un contatto può avere molte Istituzioni) e Mail, che non è una relazione 1-molti o 1-1, l'Istituzione ha un ForeignKey_mail_id.

Come posso rappresentare le relazioni di chiave esterna? Indici?

Grazie mille, spero che la mia domanda sia chiara.

+0

Non sono sicuro di capire veramente. Se una relazione non è 1-M, M-M o 1-1, che cos'è allora? – paulbailey

risposta

8

Stai pensando a CoreData in termini di DBMS che non è. Non è necessario impostare chiavi esterne per creare relazioni in CoreData. Se si desidera assegnare un messaggio di posta elettronica a un utente, è sufficiente creare una relazione tra i due ed è possibile impostare l'attributo "email" di un utente o l'attributo "utente" di un messaggio di posta elettronica. Il foreignKey e il collegamento sono tutti fatti da CoreData in background.

In un altro punto, ogni relazione è per definizione, 1-1, 1- * o -. Non sono sicuro che ci sia altra scelta ...

Quando si creano relazioni in CoreData si stanno effettivamente creando nuovi attributi per questo elemento. Ecco un esempio:

@interface User : NSManagedObject 

#pragma mark - Attributes 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *emailAddress; 

#pragma mark - Relationships 
//All to-many relationships are saved as Sets. You can add to the "emails" relationship attribute to add email objects 
@property (nonatomic, strong) NSSet  *emails; 
//All to-one relationships are saved as types of NSManagedObject or the subclass; in this case "Institution" 
@property (nonatomic, strong) Institution *institution; 

l'impostazione di questi è semplice come:

User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self.fetchedResultsController managedObjectContext]]; 
[user setName:@"Matt"]; 
[user setEmailAddress:@"[email protected]"]; 

//...Maybe i need to query my institution 
NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Institution"]; 
    [bcQuery setPredicate:[NSPredicate predicateWithFormat:@"id == %@",  institutionId]]; 
    NSArray *queryResults = [context executeFetchRequest:query error:&error]; 
[user setInstitution:[queryResults objectForId:0]]; 

//Now the user adds a email so i create it like the User one, I add the proper 
//attributes and to set it to the user i can actually set either end of the 
//relationship 
Email *email = ... 
[email setUser:user]; 

//Here i set the user to the email so the email is now in the user's set of emails 
//I could also go the other way and add the email to the set of user instead. 

Spero che questo aiuti a chiarire le cose un po '! Leggi sulla documentazione per assicurarti che CoreData sia adatto a te!

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/CoreData.pdf

Problemi correlati