开发者

Storing data on iPad

开发者 https://www.devze.com 2023-01-22 18:22 出处:网络
My application needs to store a list of airports.This list needs be setup manually i.e. its not coming from a web service.

My application needs to store a list of airports. This list needs be setup manually i.e. its not coming from a web service.

I am thinking of creating a Core Data database and then inputting this list myself. This Core db will then ship with the App.

Would you recommend any other ways of doing this? Should I perhaps rather use an开发者_运维知识库 XML file or some other way of doing it?


It all depends on the amount of data you are expecting to get. For a small amount, definitely use plists - save the hassle of setting up a database which you'll need to have for large amounts or if you need relationships different types of data.


You can use plist.
It is very easy to save list of string or other data without using core data.
See Property List Programming Guide

For example, this code will save an object (rootObj) which can be an array or a dictionary:

NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"yourFile.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj
                                                               format:NSPropertyListXMLFormat_v1_0
                                                     errorDescription:&error];
if(plistData) {
  [plistData writeToFile:plistPath atomically:YES];
}
else {
  NSLog(@"Error : %@",error);
  [error release];
}

There is some limitation on supported class (see the guide)


Plist is a good option with limited data, but if u have a lot of data you should use databases. They are good, reliable and easy to handle. changes can be maid easily and robust on various plateforms. Mainly they have two methods

  • CheckandcretaeDatabase

  • InitialiseDatabase

  • (void)CheckandcreateDatabase

{

BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];

NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"A.sqlite"];

success = [fileManager fileExistsAtPath:writableDBPath];

if (success) return; // The writable database does not exist, so copy the default to the appropriate location.

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"A.sqlite"];

success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

if (!success) {

NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); }

}

  • (void)initializeDatabase {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"AutoGraphics.sqlite"];

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)

{

//NSLog(@"Ok");

}

else

{

sqlite3_close(database);

NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));

}

}

After this u can read ur value through database with the help of easy queries.

=> Second method is to store your whole data in local XML files and just parse them to find out data according to ur need. Its also easy.

Decided on your needs and your proficiency.

0

精彩评论

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