1

以下のようなテーブルが1つあります

関係

クラブ詳細

クラブ詳細画像

CLUBの詳細が利用可能な XML が 1 つあります。すべてのタグには 1 つの値がありますが、Images タグには動的な画像が含まれています。

この後、私のオブジェクトは次のようになります

ClubDetails.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "ClubDetailsImages.h"



@interface ClubDetails : NSManagedObject

@property (nonatomic, retain) NSString * clubarea;
@property (nonatomic, retain) NSString * clubdealhere;
@property (nonatomic, retain) NSString * clubdescription;
@property (nonatomic, retain) NSString * clubdistance;
@property (nonatomic, retain) NSString * clubemail;
@property (nonatomic, retain) NSString * clubfacility;
@property (nonatomic, retain) NSString * clubfav;
@property (nonatomic, retain) NSString * clubid;
@property (nonatomic, retain) NSString * clublat;
@property (nonatomic, retain) NSString * clublogopath;
@property (nonatomic, retain) NSString * clubname;
@property (nonatomic, retain) NSString * clubphone;
@property (nonatomic, retain) NSString * cluburl;
@property (nonatomic, retain) NSString * clubvenutype;
@property (nonatomic, retain) NSString * clublong;
@property (nonatomic, retain) NSSet *clubdetailsimages;
@end

@interface ClubDetails (CoreDataGeneratedAccessors)

- (void)addClubdetailsimagesObject:(ClubDetailsImages *)value;
- (void)removeClubdetailsimagesObject:(ClubDetailsImages *)value;
- (void)addClubdetailsimages:(NSSet *)value;
- (void)removeClubdetailsimages:(NSSet *)value;

その.mファイルは次のようになります

@implementation ClubDetails

@dynamic clubarea;
@dynamic clubdealhere;
@dynamic clubdescription;
@dynamic clubdistance;
@dynamic clubemail;
@dynamic clubfacility;
@dynamic clubfav;
@dynamic clubid;
@dynamic clublat;
@dynamic clublogopath;
@dynamic clubname;
@dynamic clubphone;
@dynamic cluburl;
@dynamic clubvenutype;
@dynamic clublong;
@dynamic clubdetailsimages;


    - (void)addClubdetailsimagesObject:(ClubDetailsImages *)value {
        NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
        [self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
        [[self primitiveValueForKey:@"ClubDetailsImages"] addObject:value];
        [self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    }

    - (void)removeClubdetailsimagesObject:(ClubDetailsImages *)value {
        NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
        [self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
        [[self primitiveValueForKey:@"ClubDetailsImages"] removeObject:value];
        [self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    }

    - (void)addClubdetailsimages:(NSSet *)value {
        [self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
        [[self primitiveValueForKey:@"ClubDetailsImages"] unionSet:value];
        [self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    }

    - (void)removeClubdetailsimages:(NSSet *)value {
        [self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
        [[self primitiveValueForKey:@"ClubDetailsImages"] minusSet:value];
        [self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    }


***ClubDetailsImages.h*** looks like

    @class ClubDetails;

    @interface ClubDetailsImages : NSManagedObject

    @property (nonatomic, retain) NSString * images;
    @property (nonatomic, retain) ClubDetails *clubdetailed;


***ClubDetailsImages.m*** looks like

    @implementation ClubDetailsImages

    @dynamic images;
    @dynamic clubdetailed;



For Saving, I wrote code like this



-(void)saveClubDetails:(NSMutableArray*)allClubs{


    NSError *error;
    NSManagedObjectContext *context = [self managedObjectContext];

    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];

    [fetchRequest setEntity:[NSEntityDescription entityForName:@"ClubDetails" inManagedObjectContext:context]];

    [fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID

    NSArray *allObject = [context executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject * obj in allObject) {
        [context deleteObject:obj];
    }
    NSError *saveError = nil;
    [context save:&saveError]; // NO MORE VALUE IS DB



    for (int x = 0; x<[allClubs count]; x++) {

        ClubDetails *club = [NSEntityDescription insertNewObjectForEntityForName:@"ClubDetails"
                                                    inManagedObjectContext:context];


        ClubDetails2 *ob = (ClubDetails2*)[allClubs objectAtIndex:x];

        club.clubarea = [NSString stringWithFormat:@"%@", ob.clubarea];
        club.clubdealhere = [NSString stringWithFormat:@"%@", ob.clubdealhere];
        club.clubdescription = [NSString stringWithFormat:@"%@", ob.clubdescription];
        club.clubdistance = [NSString stringWithFormat:@"%@", ob.clubdistance];
        club.clubemail = [NSString stringWithFormat:@"%@", ob.clubemail];
        club.clubfacility = [NSString stringWithFormat:@"%@", ob.clubfacility];
        club.clubfav = [NSString stringWithFormat:@"%@", ob.clubfav];

        club.clubid = [NSString stringWithFormat:@"%@", ob.clubid];
        club.clublat = [NSString stringWithFormat:@"%@", ob.clublat];
        club.clublogopath = [NSString stringWithFormat:@"%@", ob.clublogopath];
        club.clubname = [NSString stringWithFormat:@"%@", ob.clubname];
        club.clubphone = [NSString stringWithFormat:@"%@", ob.clubphone];
        club.cluburl = [NSString stringWithFormat:@"%@", ob.cluburl];

        club.clubvenutype = [NSString stringWithFormat:@"%@", ob.clubvenutype];
        club.clublong = [NSString stringWithFormat:@"%@", ob.clublong];


        ClubDetailsImages *clubImages = [NSEntityDescription insertNewObjectForEntityForName:@"ClubDetailsImages"
                                                          inManagedObjectContext:context];


        clubImages.images = [NSString stringWithFormat:@"veer url image"];
        [club addClubdetailsimagesObject:clubImages];

    }

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


    //  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Clubs"
                                              inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

    NSArray *fetchedArray = [context executeFetchRequest:fetchRequest error:&error];

    NSLog(@"COUNT of arary is %d", [fetchedArray count]);


    for (Clubs *info in fetchedArray) {
        NSLog(@" Duaan Name  ~~~~ : %@", info.clubname);
    }


}

私は次の点で立ち往生しています

1. 1 つのクラブが 3 つ、4 つ、5 つの画像であり、配列に格納されているようなオブジェクトを CoreData に保存するにはどうすればよいですか?

2. CoreData から 1 対多の関係を取得するにはどうすればよいですか?

4

1 に答える 1