【问题标题】:How to save One-to-Many Relationship in CoreData?如何在 CoreData 中保存一对多关系?
【发布时间】:2013-09-19 04:11:46
【问题描述】:

我有一张表如下所示

我有一个 XML,其中 CLUB 的详细信息可用。每个标签都有一个值,但 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.如何在CoreData中保存这样的对象,其中一个俱乐部可能是3、4、5个存储在Array中的图像?

2.如何从 CoreData 获取这种一对多的关系?

【问题讨论】:

  • 不确定是什么问题?您只需将一张图像添加到 ClubDetailsImages 对象(手动)。添加更多迭代接收的数据并根据需要创建ClubDetailsImages 对象。获取应该像访问关系和迭代集合一样简单。

标签: iphone ios core-data sqlite one-to-many


【解决方案1】:
  1. 只需继续添加对象并将它们链接到 ClubDetails,例如,假设您有来自某个地方的图像字符串数组......

    for (NSString *image in images)
    {
        ClubDetailsImages *clubImages = [NSEntityDescription insertNewObjectForEntityForName:@"ClubDetailsImages" inManagedObjectContext:context];
    
        clubDetailsImages.image = image;
        clubDetailsImages.clubdetailed = club;
    }
    
  2. 不确定您在这里问的确切内容,但给出了一个 ClubDetails 实例:

    NSSet *images = club.clubdetailsimages;
    

另外,为什么要实现 addClubdetailsimagesObject 等。这不是必需的。使用 XCode 生成你的实体类,你会看到这些方法没有被创建。

【讨论】:

  • 因此,如果您不必实现 addClubdetailsimagesObject 等方法,谁来实现它们?如何?当我调用这些方法时发生了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多