0

I'm having a lot of difficulty with CoreData. The learning curve is very steep. I'm a pretty novice XCode user and don't know much about databases either. I've spent quite a while trying to figure out what approach to take for my program and have just been stumbling along really.

I tried to learn sqlite because that's what I thought coreData used. So, I made a huge database with many relationships in LibreOffice because it was a free Database program. Then, I realized it was in HSQL, which is different than SQLite. So then I had to export everything into .csv's and lost my relationships, then paid for a crappy program called SQLiteManager to create SQLite database tables.

Once I had everything setup, I started looking at CoreData again and I realized that CoreData doesn't allow you to use an external sqlite database! Aghh. At this point, I am pretty frustrated. So, now, I've started from scratch again with coreData and built my empty model with entities, attributes, relationships, etc. all in the managed Object form. I have checked that it is able to create the empty 'proprietary' coreData SQLite database when building, and that works.

But now, I'm trying to fill in the managedObjects, but am having trouble with the relationships

This is what one of my header files looks like.

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

@class ORGAN, SPECIMEN, SYSTEM, TISSUE;

@interface CELL : NSManagedObject

@property (nonatomic, retain) NSString * cellDescription;
@property (nonatomic, retain) NSString * cellName;
@property (nonatomic, retain) NSString * cellSID;
@property (nonatomic, retain) NSSet *organs;
@property (nonatomic, retain) NSSet *specimens;
@property (nonatomic, retain) NSSet *systems;
@property (nonatomic, retain) NSSet *tissues;
@end

@interface CELL (CoreDataGeneratedAccessors)

- (void)addOrgansObject:(ORGAN *)value;
- (void)removeOrgansObject:(ORGAN *)value;
- (void)addOrgans:(NSSet *)values;
- (void)removeOrgans:(NSSet *)values;

- (void)addSpecimensObject:(SPECIMEN *)value;
- (void)removeSpecimensObject:(SPECIMEN *)value;
- (void)addSpecimens:(NSSet *)values;
- (void)removeSpecimens:(NSSet *)values;

- (void)addSystemsObject:(SYSTEM *)value;
- (void)removeSystemsObject:(SYSTEM *)value;
- (void)addSystems:(NSSet *)values;
- (void)removeSystems:(NSSet *)values;

- (void)addTissuesObject:(TISSUE *)value;
- (void)removeTissuesObject:(TISSUE *)value;
- (void)addTissues:(NSSet *)values;
- (void)removeTissues:(NSSet *)values;

@end

and, in my applicationDelegate file, I am trying to populate my coreData Database using customs "@" delimited files in the resource sections (although, I presume I could run SQLite connections to my old DB and do the same, but that is another issue)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//cells
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:@"cells"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];

for (int i = 0 ; i < [dataRows count] ; i++)
{
    NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:@"@"];
    if ([dataElements count] >= 2)
    {
        CELL *cellMO = [NSEntityDescription  insertNewObjectForEntityForName:@"CELL" 
                                 inManagedObjectContext:[self managedObjectContext]];

        cellMO.cellName = [dataElements objectAtIndex:0];
        cellMO.cellDescription = [dataElements objectAtIndex:1];

        cellMO.organs ???????????????????????????? can't make this work.  


    }
}

It keeps breaking when I build with this line and I've tried with several different approaches. It keeps saying that cellMO.organs is expecting an NSSet, but it fails when I try to give it an NSSet. So, I'm lost. Please Help?????????

Mundi
  • 79,884
  • 17
  • 117
  • 140
esd100
  • 499
  • 1
  • 7
  • 14

1 Answers1

2

I assume that there are 0 or more strings that are organs in rest of the line. So:

if (i>1) {
   // if organ does not exist yet
   ORGAN *organMO = [NSEntityDescription  insertNewObjectForEntityForName:@"ORGAN" 
                             inManagedObjectContext:[self managedObjectContext]];
   organMO.name = [dataElements objectAtIndex:i];
   // otherwise fetch it
   [cellMO addOrgansObject:organMO];
}
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • ahhh...let me try that. It looks smart. The only thing though is that I don't have the relationships built into the list I'm reading. I was just trying to test out the addition of a relationship into the cell managed object. They should really make some sort of graphical way to do this. – esd100 May 07 '12 at 22:16
  • Yay!...it worked! Thank you very much my friend! Any advice on how to populate the CoreData database other than keep doing stuff like this in my application did launch until it's ready. I wish there were a graphical way to do it, like with the SQL tables. – esd100 May 07 '12 at 23:36
  • Let say I had a CELL table and I had an ORGANS table and they were pre-populated. How would I add the many to many relationships? for example. I want to take CELL A and say that it exists in ORGANS B,C, & E. How would I do that specifically? Your help is greatly appreciated. – esd100 May 08 '12 at 01:11
  • It's the line above `[cellMO addOrganObjects:organMO];` You just need to loop through the relevant records and execute this line. A relationship (both ways) as defined in the model will be established. – Mundi May 08 '12 at 08:36
  • Thanks so much. I think I understand. I'll give it a go and run some tests along those lines and see if I can get it to function the way I want. – esd100 May 11 '12 at 01:32