开发者

Preload a core data table with default values

I have an OSX core data app (non-document based) with several tables. I'd like to ship my app with just one of those tables preloaded with a few hundred records. The user will also be able to add more records to the pre-filled table.

What is the best way to ship my app with one of the tables pre-filled? I've seen similar answers for iOS, but I'm on OSX w开发者_StackOverflowhich doesn't appear to use sqlite for core data.

Future versions of the application may want to update this table without wiping out any of the user-defined records created with the current version.

Thank you in advance.


I have an answer too, not saying Joshua is not correct. <-- He he...double negative.

I very recently had to do this and the correct approach really depends on how much data you wish to preload?

A) If it's a lot, then yes add a pre-populated store and load that at the start

B) If it's not, then just add the data through code manually

Both have Pro's and Con's. The biggest issue with A) is that when you go to update your model, then you will have to re-populate a new template. To me this can be an administrative nightmare.

So the next thing you might wonder is how to tell when to do either A) or B)? The answer is in the metadata of the persistent store. You really should only have one store, so let's assume this is the case. What I do is just get the metadata for "the store" and if my dict object does not return YES, then populate my table. If the save succeeds, then update the metadata of "the store" with a NSNumber BOOL value of YES.

I recommend using the class methods for NSPersistentStoreCoordinator. This way you are not required to perform another save on your context.

Code:

#pragma mark - Core Data Meta Data:

- (NSDictionary *) persistentStoreMetaData {

    if ( ![_coreDataStore isExists] )
        return [NSDictionary dictionary];

    NSError * error = nil;
    NSDictionary * dict = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType 
                                                                                     URL:_coreDataStore 
                                                                                   error:&error];
    if ( error ) {
        REPORT( errReportErrErrorStoreMetaDataGet );
        return [NSDictionary dictionary];
    }

    return dict;
}

- (void) setPersistentStoreMetaData:(NSDictionary *)metaData {

    if ( ![_coreDataStore isExists] ) return;

    NSError * error = nil;
    [NSPersistentStoreCoordinator setMetadata:metaData 
                     forPersistentStoreOfType:NSSQLiteStoreType 
                                          URL:_coreDataStore 
                                        error:&error];
    if ( error ) 
        REPORT( errReportErrErrorStoreMetaDataSave );
}


Store a template data file in your application's resources. If the user has no working data file, copy the template into place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜