我有一张表如下所示
我有一个 XML,其中提供了CLUB的详细信息。每个标签都有一个值,但 Images 标签包含动态图像。
在此之后,我的对象如下所示
俱乐部详情.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 获取一对多的关系?