iOS Advanced Programming II: Data storage and IO for iOS

Source: Internet
Author: User
Tags list of attributes sqlite database

One, application sandbox

The iOS application function reads and writes files under the file area allocated by the system to the application, which is the application sandbox. All non-code files such as: Pictures, sounds, images, etc. are stored here.

Command+shift+g command in Mac, then enter users/username/library command to enter the library, then go to Application Support/iphone simulator/version/applications folder, Each of these folders corresponds to each application.

Documents: In addition to nsuserdefaults-based preference settings, the application's data and files are stored in this directory

Library: nsuserdefaults-based preference parameters are saved under Library/preferences

tmp: The application stores temporary files, itunes does not sync the data in iOS sync, and when the application does not need these files, it should be deleted to avoid space consumption.

Get the Documents path:

1 nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask,yes); 2 nsstring *dd = [paths Objectatindex:0];

Get the TMP directory:

1 nsstring *temppath = Nstemporarydirectory ();

So what kind of hairstyle do we use when we save the data?
When saving data of small amount of data, you can use Nsarray or nsdictionary, call Writetofile:atomically: method to write a file, read the contents of the file when using.

When you save data with large amounts of data, you can choose to use Sqllite,ios to provide a coredata framework.

Second, application parameters and user default settings

1. Using Settings Bundle

Settings Bundle is a special set of files in an application that holds a simple variety of configuration information. If you use the Settings app with settings Bundle,ios, your app will be displayed

2. Use Nsuserdefaults to read and save application parameters

Nsuserdefaults is a singleton class, each application has only one Nsuserdefaults object, Settings the parameters set by the bundle, and can also be read and set by Nsuserdefaults

Ways to get Nsuserdefaults:

1 nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

After you get the Nsuserdefaults object, you can get and set the application parameters

Xxforkey: (NSString *) key =>xx denotes various types, gets the value according to key

Setbool: (XXX) value forkey: (NSString *) key = = Set parameter

After setting the parameters, call the Synchronize method to save.

3. Attribute List

The list of attributes is the method by which the article begins to write data to a file using Nsarray and Nsdictionary.

However, there are some restrictions that only the following types of values can be saved to Nsarray and nsdictionary in order to directly invoke the WriteToFile method to perform the Save:

Nsarray, Nsmutablearray, Nsdictionary, Nsmutabledictionary, NSData, Nsmutabledata, NSString, NSMutableString, NSValue, NSNumber

If Nsarray and Nsdictionary Save Our custom classes, you will not be able to invoke the WriteToFile method directly to perform the save. (You can consider saving by using the object archive method)

1 //Use the attribute list to save 3 user's account password2Accountlist =[[Nsmutablearray alloc] init];3 [Accountlist addobject:[nsdictionary4Dictionarywithobjects:[nsarray arraywithobjects:@"LoginName",@"loginpwd", Nil]5Forkeys:[nsarray arraywithobjects:@"305213781",@"123123", Nil]];6 [Accountlist addobject:[nsdictionary7Dictionarywithobjects:[nsarray arraywithobjects:@"LoginName",@"loginpwd", Nil]8Forkeys:[nsarray arraywithobjects:@"475782389",@"123456", Nil]];9 [Accountlist addobject:[nsdictionaryTenDictionarywithobjects:[nsarray arraywithobjects:@"LoginName",@"loginpwd", Nil] OneForkeys:[nsarray arraywithobjects:@"330577588",@"123456789", Nil]]; A [accountlist writetofile:[self FilePath] atomically:yes]; -      -     //use Uiactionsheet to prompt the user to save successfully theUiactionsheet *sheet =[[uiactionsheet alloc] Initwithtitle:@"saved successfully" Delegate: Nil Cancelbuttontitle:nil Destructivebuttontitle:@"Determine"Otherbuttontitles:nil, nil]; -[Sheet ShowInView:self.view];

4. Using Sqlite3 Database

1) Add Libsqlite3.dylib to the project, which is a native C-function library

Common functions:

int Sqlite3_close (SQLITE3 *): Closes the data connection represented by Sqlite3 * and frees the underlying database connection resource. Before calling this function, you must first call the Sqlite3_finalize () function, call the Sqlite3_blob_close () function to close all the BLOB processors, or you will return Sqlite_busy

int sqlite3_exec (sqlite3*,const char *sql, int (*callback) (void*, int, char**,char**), void *,char **errmsg): Used to execute SQL statements with no return value

Sqlite3_int64 Sqlite3_last_insert_rowid (sqlite3*): Returns the ID of the last inserted row for the database represented by Sqlite3

int Sqlite3_changes (SQLITE3*): Returns the number of affected rows after executing a DML statement

void Sqlite3_interrupt (sqlite3*): Interrupts a long-executing query statement

int sqlite3_complete (const char *sql): Used to determine if the SQL statement is complete

int Sqlite3_open (const char * filename,sqlite3 * * ppdb): Open a link to the filename file and have the ppdb parameter reference the Open database connection

const char *sqlite3_sql (sqlite3_stmt *pstmt): Used to extract the SQL statements wrapped in SQLITE3_STMT (precompiled SQL statement-produced results)

Wait a minute... It's too much to list.

The approximate steps to manipulate the SQLite database are as follows:

1. Call the Sqlite3_open method to open the connection to the database

2. Execute the statement

3. sqlite3_close function Close Database connection

2) using the core data framework

The Core data framework is a purely object-oriented framework that allows developers to persist the SQLite database in an object-oriented manner. The core data can be persisted in the form of a SQLite database, either XML or memory.

The core concept for core data is the entity, the model object managed by core data, which must be an instance of the Nsmanagedobject class or its subclasses.

The core APIs in a core data application include the following:

managed Object Model (Nsmanagedobjectmodel): This object is responsible for managing all entities of the entire application and the association between entities. When developers use Xcode's graphical interface to design an association between entities and entities, the object needs to be used to load and manage the applied managed object model

Persistent Storage Coordinator (Nspersistentstorecoordinator): Responsible for managing underlying storage files, such as SQLite databases, etc.

Managed Object Context (Nsmanagedobjectcontext): This object is at the core of core data, and additions and deletions need to be

iOS Advanced Programming II: Data storage and IO for iOS

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.