What I recommend is to setup your database model in xcode, then when you have done that... choose the entitys and choose from the menu File > New File. Then choose the "Managed Object Class" from the "Cocoa touch class". After "Next" choose where to save the files, and at the next step XCode will ask you which entitys should be generated to files.
After you have done that, you can implemend the functions you need into your e.g. you delegate. My rekommendation is to leave your existing stuff as it is and use the core data classes as their own. Just pull the data you need from you existing classes/arrays and put them in to the database as you need them. When retrieving, the otherway around... get them from the DB and add them to your functions / classes.
Example from one of my projects:
The .h file
@class quicklistSet;
@interface rankedAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
[...]
NSMutableArray *_searchHistory;
NSMutableArray *_quickList;
}
[...]
@property (nonatomic, retain) NSMutableArray *_searchHistory;
@property (nonatomic, retain) NSMutableArray *_quickList;
/* Quicklist functions */
- (void)addToQuicklist:(quicklistSet *)theQuicklistSet;
- (BOOL)checkIfQuicklistExists:(quicklistSet*)theQuicklistSet;
- (NSMutableArray *)getQuicklists;
- (void)deleteQuicklist:(NSNumber*)theAppId;
@end
The .m file
#import "quicklistSet.h"
#import "quicklist.h"
@implementation rankedAppDelegate
@synthesize window;
@synthesize tabBarController;
@synthesize _searchHistory, _quickList;
[...]
/* Quicklist functions */
- (void)addToQuicklist:(quicklistSet *)theQuicklistSet
{
BOOL exists = [self checkIfQuicklistExists:theQuicklistSet];
if(!exists)
{
quicklist *theQuicklist = (quicklist *)[NSEntityDescription insertNewObjectForEntityForName:@"quicklist"
inManagedObjectContext:self.managedObjectContext];
[theQuicklist setAppName:[theQuicklistSet _appName]];
[theQuicklist setAppId:[theQuicklistSet _appId]];
[theQuicklist setAppImage:[theQuicklistSet _appImage]];
[theQuicklist setCountryId:[theQuicklistSet _countryId]];
[theQuicklist setCategoryId:[theQuicklistSet _categoryId]];
[theQuicklist setLastCheck:[theQuicklistSet _lastCheck]];
[theQuicklist setLastRank:[theQuicklistSet _lastRank]];
[_quickList addObject:theQuicklist];
[self saveAction];
}
else {
NSLog(@"Existing quicklistSet: %@", [theQuicklistSet _appName]);
}
}
- (BOOL)checkIfQuicklistExists:(quicklistSet*)theQuicklistSet
{
// Get the categories
NSMutableArray *quicklistArray = [self getQuicklists];
BOOL exists = NO;
for(quicklist *dbQuicklist in quicklistArray)
{
if([[dbQuicklist appId] isEqualToNumber:[theQuicklistSet _appId]])
{
exists = YES;
continue;
}
}
return exists;
}
- (NSMutableArray *)getQuicklists
{
if(_quickList == NULL)
{
NSLog(@"Array is null");
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"quicklist"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [[self.managedObjectContext
executeFetchRequest:fetchRequest error:&error] retain];
NSMutableArray *returnArray = [[[NSMutableArray alloc] initWithArray:items] retain];
_quickList = returnArray;
[fetchRequest release];
}
else {
NSLog(@"Not null. Count: %d", [_quickList count]);
}
return _quickList;
}
- (void)deleteQuicklist:(NSNumber*)theAppId
{
NSLog(@"Delete row");
// Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller's context.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"quicklist"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"appId=%@",theAppId];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *items = [self.managedObjectContext
executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
if([items count] > 0)
{
NSManagedObject *eventToDelete = [items objectAtIndex:0];
[self.managedObjectContext deleteObject:eventToDelete];
[self saveAction];
}
}
/* END Quciklist functions */
[...]
@end
EDIT:
The quicklistSet was my existsing class, the quicklist is my coredata class.
I hope this helped.
Regards, Paul Peelen