How to access data in a one-to-many relationship?

I just bought my first training book 2 months ago, so I know it's probably pretty simple, but I'm trying to learn how to use Core Data, and I can't decide how to extract data from an object in a one-to-many relationship. I spent all day searching on Google and reading Apple docs, but can't find a really simple explanation.

I installed my main data model with the following objects:

The “trip” has a one-to-many relationship from the “checklists” in ...

The checklist has a one-to-many relationship from the "checklist" for ...

"ChecklistItem".

Then in the App Delegate app in applicationDidFinishLaunching: I added the following simple code that just creates some managed objects and then displays them in the console:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // TEST CORE DATA - MAKE OBJECTS
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *trip = [NSEntityDescription insertNewObjectForEntityForName:@"Trip" inManagedObjectContext:context];
    [trip setValue:@"Test Trip" forKey:@"tripName"];

    NSManagedObject *checklist = [NSEntityDescription insertNewObjectForEntityForName:@"Checklist" inManagedObjectContext:context];
    [checklist setValue:@"Test Checklist" forKey:@"checklistName"];


    NSManagedObject *checklistItem = [NSEntityDescription insertNewObjectForEntityForName:@"ChecklistItem" inManagedObjectContext:context];
    [checklistItem setValue:@"Test Checklist Item" forKey:@"checklistItemName"];
    [checklistItem setValue:[NSNumber numberWithBool:TRUE] forKey:@"checklistItemChecked"];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

    // TEST CORE DATA - LIST OBJECTS
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Trip" inManagedObjectContext:context]];

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *info in fetchedObjects) {
        NSLog(@"Trip Name: %@", [info valueForKey:@"tripName"]);

        NSManagedObject *aChecklist = [info valueForKey:@"checklists"];
        NSLog(@"Checklist Name: %@", [aChecklist valueForKey:@"checklistName"]);

        NSManagedObject *aChecklistItem = [aChecklist valueForKey:@"checklistItems"];
        NSLog(@"Checklist Item Name: %@", [aChecklistItem valueForKey:@"checklistItemName"]);
        NSLog(@"Checklist Item Checked: %@", [aChecklistItem valueForKey:@"checklistItemChecked"] ? @"Yes it is!" : @"No It Not!");
    }
    [fetchRequest release];

    [self.window makeKeyAndVisible];

    return YES;
}

When I run the program, I get the following in the terminal:

2011-02-21 18: 01: 14.144 BYG [38204: 207] Trip name: test drive 2011-02-21 18: 01: 14.146 BYG [38204: 207] Checklist Title: {()} 2011-02-21 18: 01: 14.147 BYG [38204: 207] Checklist Product Name: {()} 2011-02-21 18: 01: 14.147 BYG [38204: 207] Checklist Checked Item: Yes, that!

, ( ) . , , : , , . ?

, . , :)

-------------------------- EDIT ------

! , , ! , , , :

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.   
    // TEST CORE DATA - MAKE OBJECTS
    NSManagedObjectContext *context = [self managedObjectContext];

    Trip *newTrip = [Trip addEntityForContext:context];
    newTrip.tripName = @"Test Trip with addEntity"; 

    Checklist *newChecklist = [NSEntityDescription insertNewObjectForEntityForName:@"Checklist" inManagedObjectContext:context];
    [newTrip addChecklistsObject:newChecklist];
    newChecklist.checklistName = @"Test Checklist"; 

    ChecklistItem *newChecklistItem = [NSEntityDescription insertNewObjectForEntityForName:@"ChecklistItem" inManagedObjectContext:context];
    [newChecklist addChecklistItemsObject:newChecklistItem];
    newChecklistItem.checklistItemName = @"Test Checklist Item";
    newChecklistItem.checklistItemChecked = [NSNumber numberWithBool:TRUE];

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

    // TEST CORE DATA - LIST OBJECTS
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Trip" inManagedObjectContext:context]];    
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (Trip *info in fetchedObjects) {
        NSLog(@"Trip Name: %@", info.tripName);

        NSSet *aChecklistSet = [[NSSet alloc]initWithSet:info.checklists];
        Checklist *aChecklist = [aChecklistSet anyObject];
        NSLog(@"Checklist Name: %@", aChecklist.checklistName);

        NSSet *aChecklistItemSet = [[NSSet alloc]initWithSet:aChecklist.checklistItems];
        ChecklistItem *aChecklistItem = [aChecklistItemSet anyObject];
        NSLog(@"Checklist Item Name: %@", aChecklistItem.checklistItemName);
        NSLog(@"Checklist Item Checked: %@", aChecklistItem.checklistItemChecked ? @"Yes it is!" : @"No It Not!");
    }
    [fetchRequest release];

    [self.window makeKeyAndVisible];

    return YES;
}

( addEntity, , , , , ).

+3
1

. () . , :
MVC

, coredatamodel, ( Entity) . NSManagedObject. .

.

:

+ (CheckListItem *) addEntity {
   return (CheckListItem *)[NSEntityDescription insertNewObjectForEntityForName:@"ChecklistItem" inManagedObjectContext:context];
}

. 1) : didFinishLaunchingWithOptions: 2) singleton . ( )

, :

CheckListItem *newItem = [CheckListItem addEntity];
CheckList *newList = [CheckList addEntity];

newItem.name = @"MyCheckListItemName";
newList.name = @"MyListName";

, :

newItem.checkList = newList;

:

[newList addCheckListItemsObject:newItem];
+2

Source: https://habr.com/ru/post/1793662/


All Articles