On Feb 20, 2011, at 10:23 PM, Shane wrote:
...

Shane, 
you usually need these initial steps for virtually any Core Data application:
(please refer to corresponding documentation for each CoreData class and method 
mentioned!)

@interface SomeDelegateOrControllerOrElse (CoreDataAdditions) {
    NSManagedObjectContext*         managedObjectContext_;
    NSManagedObjectModel*           managedObjectModel_;
    NSPersistentStoreCoordinator*   persistentStoreCoordinator_;
}

@property (nonatomic, retain, readonly) NSURL* storeURL;
@property (nonatomic, retain, readonly) NSManagedObjectContext* 
managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel* 
managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator* 
persistentStoreCoordinator;

@end


@implementation  SomeDelegateOrControllerOrElse (CoreDataAdditions)

// property storeURL
- (NSURL*) storeURL {
    // ...
    return aURL;  // return a URL where you want to have your persistent store 
saved.
}

- (NSManagedObjectContext*) managedObjectContext  {    
    if (managedObjectContext_ == nil) {
        NSPersistentStoreCoordinator* coordinator = [self 
persistentStoreCoordinator];
        if (coordinator != nil) {
            managedObjectContext_ = [[NSManagedObjectContext alloc] init];
            [managedObjectContext_ setPersistentStoreCoordinator:coordinator];
        }
    }
    return managedObjectContext_;
}

- (NSManagedObjectModel*) managedObjectModel  {    
    if (managedObjectModel_ == nil) {
        NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"MyModel" 
withExtension:@"momd"];
        managedObjectModel_ = [[NSManagedObjectModel alloc] 
initWithContentsOfURL:modelURL];    
    }
    return managedObjectModel_;
}

- (NSPersistentStoreCoordinator* )persistentStoreCoordinator  {
    if (persistentStoreCoordinator_ == nil) {
        NSError* error = nil;
        persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] 
initWithManagedObjectModel:[self managedObjectModel]];
        if (![persistentStoreCoordinator_ 
addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:nil 
                                                                 
URL:self.storeURL 
                                                             options:nil 
                                                               error:&error]) 
        {
            // Replace this implementation with code to handle the error 
appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }    
    }
    return persistentStoreCoordinator_;
}

...
@end

As others already suggested, please read the corresponding documentation.

especially:
the "Core Data Programming Guide"
and 
<http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreDataUtilityTutorial/CoreDataUtilityTutorial.pdf>
This sample shows the very basics, without any additional fuzz.

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to