Four Common Data Persistence methods in IOS and four data persistence methods in ios

Source: Internet
Author: User

Four Common Data Persistence methods in IOS and four data persistence methods in ios

  • (1) attribute list: simple, applicable only to small data volumes
  • (2) object Archiving: encryption and storage are serialized and can only be applied to small data volumes.
  • (3) SQLite: SQLite is portable, easy to use, small, efficient, and reliable.
  • (4) CoraData: Core Data uses SQLite to store Data, but it does not need to write any SQL statements.
1. property list: container object --> property list: Save the array to the sandbox path.

2. Object Archiving: serialize data

3. SQLite
  • -(Void) copyDBFile

    {

    NSFileManager * manager = [NSFileManager defamanager manager];

    // If the file exists, it will not be copied

    If ([manager fileExistsAtPath: [self databasePath]) {

    Return;

    }

    NSString * atPath = [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: nil];

    // Copy the database file to the sandbox path

    [Manager copyItemAtPath: atPath toPath: [self databasePath] error: nil];

    }


    // Obtain the path of the database file

    -(NSString *) databasePath

    {

    // NSLog (@ "% @", [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: nil]);

    Return [NSHomeDirectory () stringByAppendingFormat: @ "/Library/% @", kDatabaseName];

    // Return [[NSBundle mainBundle] pathForResource: kDatabaseName ofType: nil];

    }


    // Add a data entry

    -(BOOL) addUser :( User *) user

    {

    // Database object

    Sqlite3 * mysqlite = nil;


    // 1. Open the database

    // Filename: Path of the database file (string of C)

    // Sqlite3: database object to be executed

    Int openResult = sqlite3_open ([[self databasePath] UTF8String], & mysqlite );

    // If the open function is successfully executed, 0 and SQLITE_ OK are returned.

    If (openResult! = SQLITE_ OK ){

    NSLog (@ "database opening failed ");

    Return NO;

    }

    // 2. Prepare SQL statements

    // You can regard the content stored in sqlite3_stmt * as an SQL statement.

    Sqlite3_stmt * stmt = nil;

    // Construct an SQL statement

    NSString * SQL = [NSString stringWithFormat: @ "insert into UserTable (username, password, phone, age) values (\" % @ \ ", \" % @\", \ "% @ \", % ld) ", user. username, user. password, user. phone, user. age];

    // Sqlite3: database object to be executed

    // ZSql: SQL statement

    // Stmt: Object saved by the statement

    Sqlite3_prepare (mysqlite, [SQL UTF8String],-1, & stmt, NULL );

    // 3. Execute the SQL statement

    Int stepResult = sqlite3_step (stmt );

    If (stepResult = SQLITE_ERROR | stepResult = SQLITE_MISUSE ){

    NSLog (@ "execution failed ");

    }

    // 4. SQL statement completion

    Sqlite3_finalize (stmt );

    // 5. Close the database

    Sqlite3_close (mysqlite );

    Return YES;

    }

    4. CoreData

    -(Void) openDataBase

    {

    // 1. Load the data model file xcdatamodeld

    NSURL * url = [[NSBundle mainBundle] URLForResource: @ "DataModel" withExtension: @ "momd"];

    // NSManagedObjectModel is used to load the data model file.

    NSManagedObjectModel * dataModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: url];

    // 2. Open the database file corresponding to the model file

    // Create a coordinator object and use the Coordinator to manage database files

    NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: dataModel];

    // Specify the location of the database file in the sandbox

    NSString * storePath = [NSHomeDirectory () stringByAppendingString: @ "/Documents/DataStore. sqlite"];

    NSURL * storeURL = [NSURL fileURLWithPath: storePath];

    // Open a data file PersistentStore --> database file

    /*

    1. If the file does not exist, create a new database file.

    2. If the file exists, open the file directly.

    */

    NSError * error = nil;

    [Psc addPersistentStoreWithType: NSSQLiteStoreType // The generated database file format is SQLite

    Configuration: nil

    URL: storeURL // path for storing database files

    Options: nil

    Error: & error];

    If (error ){

    NSLog (@ "database file opening failed ");

    } Else

    {

    NSLog (@ "database opened successfully ");

    }

    // 3. Operate the database

    Context = [[NSManagedObjectContext alloc] init];

    // Specifies which coordinator the context uses to Operate Database Files

    Context. persistentStoreCoordinator = psc;

    }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.