2016-01-25 21 views
6

Ho creato un modello di dati in questo modo:risparmio, la cancellazione e il recupero dei dati da uno a molti dati fondamentali di relazione

ho questo codice per una richiesta di recupero:

func roundFetchRequest() -> NSFetchRequest { 
    let fetchRequest = NSFetchRequest(entityName: "Customer") 
    print("Check here: \(myRoundIndexPath)") 
    //let predicate : NSPredicate = NSPredicate(format: "custRoundRel = %@", frc2.objectAtIndexPath(myRoundIndexPath!) as! RoundName) //ASSUME THIS IS CORRECT 
    let sortDescriptor = NSSortDescriptor(key: "c2fna", ascending: true) 
    //fetchRequest.predicate = predicate 
    fetchRequest.sortDescriptors = [sortDescriptor] 
    return fetchRequest 
} 

Il mio codice commentato non dà un errore, ma non riesco a salvare un cliente nell'istanza RoundName. Quando salvi un cliente con i suoi attributi, ho utilizzato questo codice:

func newCust() { 
    let cont = self.context 
    let newCustomer = NSEntityDescription.entityForName("Customer", inManagedObjectContext: cont) 
    let aCust = Customer(entity: newCustomer!, insertIntoManagedObjectContext: cont) 

    aCust.c2fna = firstName.text 
    aCust.c3lna = lastName.text 
    aCust.c4tel = tel.text 
    aCust.c5mob = mob.text 
    aCust.c6ema = email.text 
    aCust.c7hsn = houseNo.text 
    aCust.c8fir = street.text 
    aCust.c9sec = secondLine.text 
    aCust.c10ar = area.text 
    aCust.c11pc = postcode.text 
    aCust.c12cos = cost.text 
    aCust.c13fq = frequencyNumber.text 
    aCust.c14fqt = frequencyType.text 
    let DF = NSDateFormatter() 
    aCust.c15das = DF.dateFromString(startDate.text!) 
    //Do Pics in a minute & next date in a minute 
    aCust.c17notes = notes.text 

    //print("Desc = \(picRound.image?.description)") 

    do { 
     try context.save() 
     print("Save Successful") 
    } catch { 
     print("Save Unsuccessful") 
    } 
} 

Qual è il codice per collegare questo cliente al Round corretto?

Grazie, sono molto nuovo per i dati di base e apprezzerei davvero qualsiasi aiuto.

risposta

1

Sì, è possibile utilizzare un predicato sulla vostra richiesta di recupero, con un formato simile

NSPredicate(format:"custRoundRel = %@", xxxx) 

dove xxxx è l'istanza Round.

È anche possibile utilizzare la relazione roundCustRel in base a ciò che si desidera fare con le istanze Customer e quante ci sono.

+0

Grazie a @Wain, così quando salvi un'entità cliente con i relativi attributi come faccio a collegarlo al nome del round corretto? – agf119105

+0

è necessario avere quell'istanza 'Round', o già o recuperarla per nome, quindi basta impostarla sulla relazione:' yyyy.custRoundRel = xxxx' (dove 'yyyy' è la tua nuova istanza' Cliente' – Wain

-1

Gli oggetti Customer si creano nello stesso modo in cui si creano altri oggetti gestiti. Per collegare un cliente con l'oggetto Round corretto, è sufficiente impostare la relazione di uno (Core Data imposterà automaticamente la relazione inversa).

newCustomer.round = round 
// or, with your arcane attribute names 
newCustomer.custRoundRel = theDesiredRoundObject 

Per raggiungere i clienti di un round specifico, non è necessario richiedere richieste o predicati.

round.customers 
// or, with your arcane attribute names 
round.roundCustRel