views:

611

answers:

2

I've here a CoreData app (no document-based), 1 entity and 1 tableview for edit/add/remove "instances" of the entity. Now I can manual add and save but I would like to

a) save automaticly changes
b) add automaticly some "instances" with the first start.

I think a) can be solved with NSNotifications. But which to use by entities?

Any ideas how to solve a) or b)?

Thanks for every answer. =)

A: 

Have a look at this thread on the Apple Mailing lists regarding autosave and Core Data.

Abizern
+1  A: 

Autosave can be a bit trickier than you'd first expect sometimes, since there may be times when your application data is in an invalid state (for instance, when the user is editing an entity) and either cannot be saved or it would not make sense to save. There's no easy setAutosaves:YES property unfortunately, so you'll have to implement it yourself. Using a notification to save after certain actions is one way to do it, you could also set up a timer to save periodically if it makes sense for your application.

To populate an empty data file, just check to see if the data store is empty at launch (applicationDidFinishLaunching and awakeFromNib are two possible places to put this), and if it is insert some entities as normal. The only tricky part is disabling undo management during the process. Here's an example from one of my applications:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
{       
    NSURL *fileURL = [NSURL fileURLWithPath:[self.applicationSupportFolder stringByAppendingPathComponent:WLDataFileName]];

    NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];    
    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:nil error:NULL];
    [context setPersistentStoreCoordinator:coordinator];
    [request setEntity:[NSEntityDescription entityForName:@"Shelf" inManagedObjectContext:context]];

    if ( [context countForFetchRequest:request error:NULL] == 0 )
     [self _populateEmptyDataStore:context];

    _managedObjectContext = [context retain];

    [request release];
    [coordinator release];
    [context release];

    // finish loading UI, etc...
}

- (void)_populateEmptyDataStore:(NSManagedObjectContext *)context;
{
    [[context undoManager] disableUndoRegistration];

    WLSmartShelfEntity *allItems = [NSEntityDescription insertNewObjectForEntityForName:@"SmartShelf" inManagedObjectContext:context];
    WLSmartShelfEntity *trash = [NSEntityDescription insertNewObjectForEntityForName:@"SmartShelf" inManagedObjectContext:context];

    allItems.name = NSLocalizedString( @"All Items", @"" );
    allItems.predicate = [NSPredicate predicateWithFormat:@"isTrash = FALSE"];
    allItems.sortOrder = [NSNumber numberWithInteger:0];
    allItems.editable = [NSNumber numberWithBool:NO];

    trash.name = NSLocalizedString( @"Trash", @"" );
    trash.predicate = [NSPredicate predicateWithFormat:@"isTrash = TRUE"];
    trash.sortOrder = [NSNumber numberWithInteger:2];
    trash.editable = [NSNumber numberWithBool:NO];

    [context processPendingChanges];
    [[context undoManager] enableUndoRegistration];

    DebugLog( @"Filled empty data store with initial values." );
}
Marc Charbonneau