[Erledigt] CoreData und Relationships (Xcode Template)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • [Erledigt] CoreData und Relationships (Xcode Template)

    Hallo!

    Ich bin seit Tagen dabei, zu meinem Projekt CoreData hinzuzufügen.
    Zu Weihnachten habe ich das Buch "Apps entwicklen für iPhone und iPad" bekommen, wo aber nicht so gut auf Relationships eingegangen wird.
    Auch bei Google bin ich noch nicht fündig geworden...

    Mein Problem:

    Ich weiß nicht so genau wie ich mit einer Oen-To-Many Relationship umgehe...

    Meine Situation:

    Ich habe ein Data Model angelegt mit folgenden Entitäten:

    • Rennen
    • Lauf
    Bei 1 Rennen finden mehrere Läufe statt und ein Lauf findet.
    Also habe ich in der Entiät Rennen folgende Relationship erstellt: Name: lauf / Destination: Lauf / Inverse: Rennen / Optional / To-Many Relationship / Delete Rule: Cascade
    Für die Entität Lauf habe ich folgende Relation erstellt: Name: rennen / Destination: Rennen / Inverse: Lauf / Delete Rule: Nullify
    Was ich nun gemacht habe ist ein neues Projekt erstellt mit "Use CoreData for Storage" und habe den komplette XCode Template Code in mein Projekt integriert.
    Ein Rennen kann ich wunderbar meinem ManagedObjectContext hinzufügen... Mit dem Lauf wird es aber schon schwieriger...
    Was ich nicht hinbekomme und verstehe ist wie ich beim Lauf das Rennen zuweise.
    Mein Code Dazu:
    Rennen speichern:

    Quellcode

    1. - (IBAction)saveRennen:(id)sender {
    2. // Create a new instance of the entity managed by the fetched results controller.
    3. NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    4. //NSLog(@"%@", context);
    5. NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    6. //NSLog(@"%@", entity);
    7. NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    8. // If appropriate, configure the new managed object.
    9. // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    10. [newManagedObject setValue:self.textfieldRennen.text forKey:@"name"];
    11. [newManagedObject setValue:[NSDate date] forKey:@"datum"];
    12. // Save the context.
    13. NSError *error = nil;
    14. if (![context save:&error]) {
    15. /*
    16. Replace this implementation with code to handle the error appropriately.
    17. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    18. */
    19. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    20. abort();
    21. }
    22. [self.navigationController popViewControllerAnimated:TRUE];
    23. }
    Alles anzeigen

    Lauf speichern:

    Quellcode

    1. -(void)saveZeit
    2. {
    3. // Create a new instance of the entity managed by the fetched results controller.
    4. NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    5. NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    6. NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    7. // If appropriate, configure the new managed object.
    8. // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    9. [newManagedObject setValue:self.textfieldLaufnummer.text forKey:@"name"];
    10. NSDate *date = [self.datepickerZeit date];
    11. [newManagedObject setValue:date forKey:@"zeit"];
    12. // Save the context.
    13. NSError *error = nil;
    14. if (![context save:&error]) {
    15. /*
    16. Replace this implementation with code to handle the error appropriately.
    17. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    18. */
    19. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    20. abort();
    21. }
    22. [self.navigationController popViewControllerAnimated:YES];
    23. }
    Alles anzeigen

    Über einwenig Hilfe und coole Tipps würde ich mich sehr freuen!
    Gruß
    Marvin

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Marvao ()