开发者

what is the best way to store data on iphone with a set of preloaded values which must be altered from the program

What is the best way to store data on iphone with a set of preloaded values and that must 开发者_如何学Cbe changed from the program also?

Can we preload values into a coredata database.if yes how


it is possible with coredata.....check this link to use coredata


If using Core Data, you can detect if your core data backing file exists before creating the NSPersistentStoreCoordinator, and if not either copy a default version out of your bundle into place before creating the persistent store coordinator or use code to insert the necessary objects after creating the NSManagedObjectContext. For the latter, that might look something like this:

BOOL insertDefaultObjects = NO;
if (managedObjectContext == nil) {
    if (persistentStoreCoordinator == nil) {
        NSString *storePath = /* ... */;
        insertDefaultObjects = ![[NSFileManager defaultManager] fileExistsAtPath:storePath];

        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
        /* ... and so on ... */
    }

    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

    if (insertDefaultObjects) [self insertDefaultObjectsIntoContext:managedObjectContext];
}

insertDefaultObjectsIntoContext: would just use the standard Core Data methods to create the necessary objects and save them into the Core Data store.

If you're using some other sort of data storage, e.g. an xml file, a plist, etc., the same general idea holds: check if the file exists in your Documents or Application Support directory, or check a "default values copied" key in NSUserDefaults if the file can be deleted by the user, and copy the default version out of your application bundle if necessary.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜