Showing posts with label coredata. Show all posts
Showing posts with label coredata. Show all posts

Sunday, 5 June 2011

Core Data schema change migration

Place the below in your:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
method


To remove the old data store (clear out old data models):

[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];


To migrate data using Lightweight Migration


[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];


In Xcode 4 we migrate data models by:
select data model. (on left)
Apple - alt - 1 ( View - Utilities - File Inspector)
Versioned Data model (on right side of utilities window)

Monday, 11 April 2011

Core data basics

So core data. Turns out I actually like this bit of IPhone coding. It makes ORM / DB stuff really easy.

Samble code to load and sort a 'fugitive' object from an SQLLite DB



- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
bountyHunterAppDelegate *appDelegate = (bountyHunterAppDelegate*)[[UIApplicaion sharedApplication] delgate];
NSManagedObjectContext *managedObectContext = appDelegate.managedObjectContext;
// Create request object to get data
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:managedObectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error;
NSMutableArray *mutablesFetchResults = [[managedObectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutablesFetchResults == Nil) {
NSLog(@"all buggered");
}
self.items = mutableCopy;
[mutablesFetchResults release];
[request release];
}