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:
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:
Alles anzeigen
Lauf speichern:
Alles anzeigen
Über einwenig Hilfe und coole Tipps würde ich mich sehr freuen!
Gruß
Marvin
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
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
- - (IBAction)saveRennen:(id)sender {
- // Create a new instance of the entity managed by the fetched results controller.
- NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
- //NSLog(@"%@", context);
- NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
- //NSLog(@"%@", entity);
- NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
- // If appropriate, configure the new managed object.
- // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
- [newManagedObject setValue:self.textfieldRennen.text forKey:@"name"];
- [newManagedObject setValue:[NSDate date] forKey:@"datum"];
- // Save the context.
- NSError *error = nil;
- if (![context save:&error]) {
- /*
- Replace this implementation with code to handle the error appropriately.
- 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.
- */
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
- [self.navigationController popViewControllerAnimated:TRUE];
- }
Lauf speichern:
Quellcode
- -(void)saveZeit
- {
- // Create a new instance of the entity managed by the fetched results controller.
- NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
- NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
- NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
- // If appropriate, configure the new managed object.
- // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
- [newManagedObject setValue:self.textfieldLaufnummer.text forKey:@"name"];
- NSDate *date = [self.datepickerZeit date];
- [newManagedObject setValue:date forKey:@"zeit"];
- // Save the context.
- NSError *error = nil;
- if (![context save:&error]) {
- /*
- Replace this implementation with code to handle the error appropriately.
- 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.
- */
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
- [self.navigationController popViewControllerAnimated:YES];
- }
Über einwenig Hilfe und coole Tipps würde ich mich sehr freuen!
Gruß
Marvin
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Marvao ()