简体   繁体   中英

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 be changed from the program also?

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

有可能使用coredata ..... 查看此链接以使用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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM