欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解iOS的數(shù)據(jù)存儲(chǔ)

 更新時(shí)間:2016年08月01日 10:37:48   投稿:daisy  
本文介紹iOS中常用的應(yīng)用數(shù)據(jù)存儲(chǔ)方式及其詳細(xì)用法,內(nèi)容很全面和詳細(xì),對(duì)大家在IOS開發(fā)中很有幫助,下面一起來看看。

iOS應(yīng)用數(shù)據(jù)存儲(chǔ)的常用方式

    1.XML屬性列表(plist)歸檔.

    2.Preference(偏好設(shè)置).

    3.NSKeyedArchiver歸檔.

    4.SQLite3

    5.Core Data

應(yīng)用沙盒

每個(gè)iOS應(yīng)用都有自己的應(yīng)用沙盒(應(yīng)用沙盒就是文件系統(tǒng)目錄)與其他文件系統(tǒng)隔離.應(yīng)用必須待在自己的沙盒里,其他應(yīng)用不能訪問該沙盒.

模擬器應(yīng)用沙盒的根路徑在: (apple是用戶名, 6.0是模擬器版本)

/Users/apple/Library/Application Support/iPhone Simulator/6.0/Applications

或者:

 /Users/用戶名/資源庫/Application Support/iPhone Simulator/6.1/Applications

注意:

默認(rèn)情況下,模擬器的目錄是隱藏的,要想顯示出來,需要在Mac終端輸入下面的命令:

顯示Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles YES

隱藏Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles NO

應(yīng)用沙盒結(jié)構(gòu)分析

Documents:

保存應(yīng)用運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時(shí)會(huì)備份該目錄.例如游戲應(yīng)用可將游戲存檔保存在該目錄.

temp:

保存應(yīng)用運(yùn)行時(shí)所需的臨時(shí)數(shù)據(jù),使用完畢后再將相應(yīng)的文件從該目錄刪除.應(yīng)用沒有運(yùn)行時(shí),系統(tǒng)也可能會(huì)清除該目錄下的文件.iTunes同步設(shè)備時(shí)不會(huì)備份該目錄.

Library/Caches:

保存應(yīng)用運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時(shí)不會(huì)備份該目錄.一般存儲(chǔ)體積大,不需要備份的非重要數(shù)據(jù).

Library/Preference:

保存應(yīng)用的所有偏好設(shè)置,iOS的setting(設(shè)置)應(yīng)用會(huì)在該目錄中查找應(yīng)用的設(shè)置信息.iTunes同步設(shè)備時(shí)會(huì)備份該目錄.

應(yīng)用沙盒目錄的常見獲取方式:

沙盒根路徑

 NSString *home = NSHomeDirectory();

Documents:(2種方式)

利用沙盒根目錄拼接"Documents"字符串:

NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
// 不建議采用,因?yàn)樾掳姹镜牟僮飨到y(tǒng)可能會(huì)修改目錄名

利用NSSearchPathForDirectoriesInDomains函數(shù):

// NSUserDomainMask 代表從用戶文件夾下找  
// YES 代表展開路徑中的波浪字符“~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, NO);
// 在iOS中,只有一個(gè)目錄跟傳入的參數(shù)匹配,所以這個(gè)集合里面只有一個(gè)元素
NSString *documents = [array objectAtIndex:0];

tmp:

 NSString *tmp= NSTemporaryDirectory();

Library/Catches:(跟Documents類似的兩種方法)

    1.利用沙盒跟目錄拼接"Catches"字符串.

    2.利用NSSearchPathForDirectoriesInDomains函數(shù)(將函數(shù)的第一個(gè)參數(shù)改為:NSCachesDirectory即可).

Library/Preference:通過NSUserDefaults類存取該目錄下的設(shè)置信息.

下面分別詳細(xì)介紹5中數(shù)據(jù)存儲(chǔ)方式

屬性列表

屬性列表是一種XML格式的文件,拓展名為plist.

如果對(duì)象是NSString, NSDictionary, NSArray, NSData, NSNumber等類型,就可以使用:writeToFile:atomiclly:方法直接將對(duì)象寫到屬性列表文件中.

.屬性列表-歸檔NSDictionary

將一個(gè)NSDictionary歸檔到一個(gè)plist屬性列表中.

// 將數(shù)據(jù)封裝成字典 
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"15013141314" forKey:@"phone"];
[dict setObject:@"27" forKey:@"age"];
// 將字典持久化到Documents/stu.plist文件中
[dict writeToFile:path atomically:YES];

.屬性列表-恢復(fù)NSDictionary

讀取屬性列表,恢復(fù)NSDictionary對(duì)象

// 讀取Documents/stu.plist的內(nèi)容,實(shí)例化NSDictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"phone:%@", [dict objectForKey:@"phone"]);
NSLog(@"age:%@", [dict objectForKey:@"age"]);

偏好設(shè)置(Preference)

很多iOS應(yīng)用都支持偏好設(shè)置,比如保存用戶名,密碼,是否自動(dòng)登錄等設(shè)置,iOS提供了一套標(biāo)準(zhǔn)的解決方案來為應(yīng)用加入偏好設(shè)置功能.

每個(gè)應(yīng)用都有個(gè)NSUserDefaults實(shí)例,通過它來存取偏好設(shè)置.

如保存用戶名,字體大小,是否登錄

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"Cat" forKey:@"username"];
[defaults setFloat:14.0f forKey:@"text_size"];
[defaults setBool:YES forKey:@"auto_login"];

讀取上次保存的設(shè)置

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *username = [defaults stringForKey:@"username"];
float textSize = [defaults floatForKey:@"text_size"];
BOOL autoLogin = [defaults boolForKey:@"auto_login"];

注意:

UserDefaults設(shè)置數(shù)據(jù)時(shí),不是立即寫入,而是根據(jù)時(shí)間戳定時(shí)地把緩存中的數(shù)據(jù)寫入本地磁盤.所以調(diào)用了set方法之后數(shù)據(jù)有可能還沒有寫入磁盤應(yīng)用程序就終止了.出現(xiàn)此問題,可以通過調(diào)用synchornize方法強(qiáng)制寫入.

 [defaults synchornize];

NSKeyedArchiver

如果對(duì)象是NSString, NSDictionary, NSArray, NSData, NSNumber等類型,就可以直接使用:NSKeyedArchiver進(jìn)行歸檔和恢復(fù).

不是所有的對(duì)象都可以直接用這種方法進(jìn)行歸檔,只有遵守了NSCoding協(xié)議的的對(duì)象才可以.

NSCoding協(xié)議有2個(gè)方法:

.encodeWithCoder:

每次歸檔對(duì)象時(shí),都會(huì)調(diào)用這個(gè)方法.一般在這個(gè)方法里面指定如何歸檔對(duì)象中的每個(gè)實(shí)例變量.可以使用:encodeObject:forkey:方法歸檔實(shí)例變量.

.initWithCoder:
每次從文件中恢復(fù)(解碼)對(duì)象時(shí),都會(huì)調(diào)用這個(gè)方法.一般在這個(gè)方法里面指定如何解碼文件中的數(shù)據(jù)為對(duì)象的實(shí)例變量,可以使用decodeObject:forkey方法解碼實(shí)例變量.

歸檔一個(gè)NSArray對(duì)象到Documents/array.archive :

NSString *path = [NSString stringWithFormat:@"Documents/array.archive"];
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",nil];
[NSKeyedArchiver archiveRootObject:array toFile:path];

恢復(fù)(解碼)NSArray對(duì)象

 NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSKeyedArchiver-歸檔Person對(duì)象:

Person.h中:

@interface Person : NSObject <NSCoding>
@property(nonatomic,copy) NSString *name;
@property(nonatomic,assign) int age;
@property(nonatomic,assign) float height;@end

Person.m中:

 @implementation Person
-(void)encodeWithCoder:(NSCoder*)encoder{  
 [encoder encodeObject:self.name forKey:@"name"];  
 [encoder encodeInt:self.age forKey:@"age"];  
 [encoder encodeFloat:self.height forKey:@"height"];
}
-(id)initWithCoder:(NSCoder*)decoder {  
 self.name= [decoder decodeObjectForKey:@"name"];  
 self.age= [decoder decodeIntForKey:@"age"];  
 self.height= [decoder decodeFloatForKey:@"height"];  
 return self;
}

 // 歸檔(編碼)
 Person *person = [[Person alloc] init];
 person.name = @"JN";
 person.age = 22;
 person.height = 1.63f;
 [NSKeyedArchiver archiveRootObject:person toFile:path];

 // 恢復(fù)(解碼)
 Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
@end

NSKeyedArchiver-歸檔對(duì)象的注意

如果父類也遵守了NSCoding協(xié)議,請(qǐng)注意:

1.應(yīng)該在uencodeWithCoder:方法中加上一句 [super encodeWithCode:encode];確保繼承的實(shí)例變量也能被編碼,即也能被歸檔.

2.應(yīng)該在initWithCoder:方法中加上一句 self = [super initWithCoder:decoder];確保繼承的實(shí)例變量也能被解碼,即也能被恢復(fù).

NSData

使用archiveRootObject:toFile:方法可以將一個(gè)對(duì)象直接寫入到一個(gè)文件中,但有時(shí)候可能想將多個(gè)對(duì)象寫入到同一個(gè)文件中,那么就要使用NSData來進(jìn)行歸檔對(duì)象.

NSData可以為一些數(shù)據(jù)提供臨時(shí)的存儲(chǔ)空間,以便隨后寫入文件,或者存放從磁盤讀取的文件內(nèi)容.可以使用[NSMutableData data]創(chuàng)建可變數(shù)據(jù)空間.

NSData歸檔2個(gè)Person對(duì)象到同一文件中:

歸檔:

// 新建一塊可變數(shù)據(jù)區(qū)
NSMutableData *data = [NSMutableData data];
// 將數(shù)據(jù)區(qū)連接到一個(gè)NSKeyedArchiver對(duì)象
NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
// 開始存檔對(duì)象,存檔的數(shù)據(jù)都會(huì)存儲(chǔ)到NSMutableData中
[archiver encodeObject:person1 forKey:@"person1"];
[archiver encodeObject:person2 forKey:@"person2"];
// 存檔完畢(一定要調(diào)用這個(gè)方法)
[archiver finishEncoding];
// 將存檔的數(shù)據(jù)寫入文件
[data writeToFile:path atomically:YES];

恢復(fù)(解碼):

// 從文件中讀取數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfFile:path];
// 根據(jù)數(shù)據(jù),解析成一個(gè)NSKeyedUnarchiver對(duì)象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
Person *person1 = [unarchiver decodeObjectForKey:@"person1"];
Person *person2 = [unarchiver decodeObjectForKey:@"person2"];
// 恢復(fù)完畢[unarchiver finishDecoding];

利用歸檔實(shí)現(xiàn)深復(fù)制:比如對(duì)一個(gè)Person對(duì)象進(jìn)行深復(fù)制

// 臨時(shí)存儲(chǔ)person1的數(shù)據(jù)
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:person1];
// 解析data,生成一個(gè)新的Person對(duì)象
Student *person2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
// 分別打印內(nèi)存地址
NSLog(@"person1:%@", person1); // person1:0x8d3ed10>
NSLog(@"person2:%@", person2); // person2:0x8d3e2f0>

SQLite3

    1.SQLite3是一款開源的嵌入式關(guān)系型數(shù)據(jù)庫,可移植性好,易使用,內(nèi)存開銷小.

    2.SQLite3是無類型的,意味著你可以保存任何類型的數(shù)據(jù)到任意表的任意字段中.

    3.SQLite3常用的4種數(shù)據(jù)類型:text(文本字符串), integer(整型值), real(浮點(diǎn)值), blob(二進(jìn)制數(shù)據(jù)(比如文件)).

在iOS中使用SQLite3,首先要添加庫文件 'libsqlite3.dylib'和導(dǎo)入主頭文件#import<sqlite3.h>

SQL語句的特點(diǎn):

1> 不區(qū)分大小寫;

2> 每條語句都必須以分號(hào);結(jié)尾

SQL中常用的關(guān)鍵字:
pselect、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等

數(shù)據(jù)庫中不可以使用關(guān)鍵字來命名表,字段.

SQL語句種類:

1> 數(shù)據(jù)定義語句(DDL:Data Definition Language)

包括create和drop等操作 ;

在數(shù)據(jù)庫中創(chuàng)建新表或刪除表(create table或 drop table).

2> 數(shù)據(jù)操作語句(DML:Data Manipulation Language)

包括insert、update、delete等操作 ;

上面的3種操作分別用于添加、修改、刪除表中的數(shù)據(jù) .

3> 數(shù)據(jù)查詢語句(DQL:Data Query Language)

可以用于查詢獲得表中的數(shù)據(jù) ;

關(guān)鍵字select是DQL(也是所有SQL)用得最多的操作 ;

其他DQL常用的關(guān)鍵字有where,order by,group by和having創(chuàng)建.

創(chuàng)表:

create table if not exists t_student (id integer, name text, age inetger, score real) ;

刪表:

drop table if exists t_student;

插入數(shù)據(jù)(insert):

insert into t_student (name, age) values ('JN', 22) ;

注意:數(shù)據(jù)庫中的字符串內(nèi)容應(yīng)該用單引號(hào)''括住.

更新數(shù)據(jù)(updata):

pupdate t_student set name = 'jack', age = 20 ;

注意:上面的更新會(huì)將t_student表中所有記錄的name都改為jack,age都改為20;

刪除數(shù)據(jù)(delete):

delete from t_student;

會(huì)將t_student表中所有記錄都刪掉.

如果只想更新或者刪除某些固定的記錄,那就必須在DML語句后加上一些條件.示例如下:

// 將t_student表中年齡大于10 并且 姓名不等于jack的記錄,年齡都改為 5 
update t_student set age = 5 where age > 10 and name != ‘jack' ; 
// 刪除t_student表中年齡小于等于10 或者 年齡大于30的記錄 
delete from t_student where age <= 10 or age > 30 ;

查詢語句(DQL):

select * from t_student where age > 10 ; // 條件查詢條件語句:

主鍵約束:

每張表都必須有一個(gè)主鍵,用來標(biāo)識(shí)記錄的唯一性.

什么是主鍵:

主鍵(Primary Key,簡稱PK),用來唯一的標(biāo)識(shí)某一條記錄.

例如t_student可以增加一個(gè)id字段作為主鍵,相當(dāng)于人的身份證.

主鍵可以是一個(gè)字段或多個(gè)字段.

外鍵約束:

利用外鍵約束可以來建立表與表之間的聯(lián)系.

外鍵的一般情況是:一張表的某個(gè)字段引用著另一張表的主鍵字段.

打開,關(guān)閉數(shù)據(jù)庫

創(chuàng)建或打開數(shù)據(jù)庫:

// path為:~/Documents/person.db
sqlite3 *db;int result = sqlite3_open([path UTF8String], &db);

代碼解析:

sqlite3_open()將根據(jù)文件路徑打開數(shù)據(jù)庫,如果不存在,則會(huì)創(chuàng)建一個(gè)新的數(shù)據(jù)庫.如果result等于常量SQLITE_OK,則表示成功打開數(shù)據(jù)庫.

sqlite *db:一個(gè)打開的數(shù)據(jù)庫實(shí)例.

數(shù)據(jù)庫文件的路徑必須以C字符串(而非NSString)傳入.

關(guān)閉數(shù)據(jù)庫:sqlite3_close(db);

執(zhí)行不返回語句的SQL語句

char *errorMsg; // 用來存儲(chǔ)錯(cuò)誤信息
char *sql = "create table if not exists t_person(id integer primary key autoincrement, name text, age integer);";
int result = sqlite3_exec(db, sql, NULL, NULL, &errorMsg);

代碼解析:

sqlite3_exec()可以執(zhí)行任何SQL語句,比如創(chuàng)表, 更新, 插入和刪除操作.但是一般不用它執(zhí)行查詢語句,因?yàn)樗粫?huì)返回查詢到得數(shù)據(jù).

sqlite3_exec()還可以執(zhí)行的語句:

1> 開啟事務(wù):begain transaction;

2> 回滾事務(wù):rollback

3> 提交事務(wù):commit

SQLite函數(shù)總結(jié):

1.打開數(shù)據(jù)庫
int sqlite3_open(  
const char *filename,  // 數(shù)據(jù)庫的文件路徑  
sqlite3 **ppDb     // 數(shù)據(jù)庫實(shí)例
);
2.執(zhí)行任何SQL語句
int sqlite3_exec(  
sqlite3*,        // 一個(gè)打開的數(shù)據(jù)庫實(shí)例  
const char *sql,              // 需要執(zhí)行的SQL語句  
int (*callback)(void*,int,char**,char**), // SQL語句執(zhí)行完畢后的回調(diào)  
void *,                  // 回調(diào)函數(shù)的第1個(gè)參數(shù)  
char **errmsg               // 錯(cuò)誤信息
);
3.檢查SQL語句的合法性(查詢前的準(zhǔn)備)
int sqlite3_prepare_v2(
  sqlite3 *db,      // 數(shù)據(jù)庫實(shí)例  
const char *zSql,    // 需要檢查的SQL語句  
int nByte,       // SQL語句的最大字節(jié)長度  
sqlite3_stmt **ppStmt, // sqlite3_stmt實(shí)例,用來獲得數(shù)據(jù)庫數(shù)據(jù)  
const char **pzTail
);
4.查詢一行數(shù)據(jù)
int sqlite3_step(
sqlite3_stmt*); // 如果查詢到一行數(shù)據(jù),就會(huì)返回SQLITE_ROW
5.利用stmt獲得某一字段的值(字段的下標(biāo)從0開始)
double sqlite3_column_double(sqlite3_stmt*, int iCol); // 浮點(diǎn)數(shù)據(jù)
int sqlite3_column_int(sqlite3_stmt*, int iCol); // 整型數(shù)據(jù)
sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); // 長整型數(shù)據(jù)
const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); // 二進(jìn)制文本數(shù)據(jù)
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); // 字符串?dāng)?shù)據(jù)

CoreData

Core Data框架提供了對(duì)象-關(guān)系映射(ORM)的功能,即能夠?qū)C對(duì)象轉(zhuǎn)化成數(shù)據(jù),保存在SQLite3數(shù)據(jù)庫文件中,也能將保存在數(shù)據(jù)庫中的數(shù)據(jù)還原成OC對(duì)象.在次數(shù)據(jù)操作期間,不需要編寫任何SQL語句.

使用此功能,要添加CoreData.framework和導(dǎo)入主頭文件<CoreDate/CoreData.h>.

模型文件:在CoreData中,需要進(jìn)行映射的對(duì)象稱為實(shí)體(entity),而且需要使用CoreData的模型文件來描述應(yīng)用的所有實(shí)體和實(shí)體屬性.

NSManagedObject

通過Core Data從數(shù)據(jù)庫中取出的對(duì)象,默認(rèn)情況下都是NSManagedObject對(duì)象.

    NSManagedObject的工作模式有點(diǎn)類似于NSDictionary對(duì)象,通過鍵-值對(duì)來存取所有的實(shí)體屬性.

    setValue:forkey:存儲(chǔ)屬性值(屬性名為key);

    valueForKey:獲取屬性值(屬性名為key).

CoreData主要對(duì)象

NSManagedObjectContext:負(fù)責(zé)數(shù)據(jù)和應(yīng)用庫之間的交互(CRUD);

  NSPersistentStoreCoordinator:添加持久化存儲(chǔ)庫(比如SQLite數(shù)據(jù)庫);

  NSManagedObjectModel:代表Core Data的模型文件;

  NSEntityDescription:用來描述實(shí)體;

搭建CoreData上下文環(huán)境:

// 從應(yīng)用程序包中加載模型文件 
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 傳入模型,初始化
NSPersistentStoreCoordinator NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 構(gòu)建SQLite文件路徑 
NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSURL *url = [NSURL fileURLWithPath:[docs stringByAppendingPathComponent:@"person.data"]];
// 添加持久化存儲(chǔ)庫,這里使用SQLite作為存儲(chǔ)庫 
NSError *error = nil;
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
if (store == nil) { // 直接拋異常  
    [NSException raise:@"添加數(shù)據(jù)庫錯(cuò)誤" format:@"%@", [error localizedDescription]];
}
// 初始化上下文,設(shè)置persistentStoreCoordinator屬性 
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];context.persistentStoreCoordinator = psc;
// 用完之后,還是要[context release];
/*持久化存儲(chǔ)庫的類型:NSSQLiteStoreType SQLite數(shù)據(jù)庫 
NSBinaryStoreType 二進(jìn)制平面文件 
NSInMemoryStoreType 內(nèi)存庫,無法永久保存數(shù)據(jù) 
雖然這3種類型的性能從速度上來說都差不多,但從數(shù)據(jù)模型中保留下來的信息卻不一樣
在幾乎所有的情景中,都應(yīng)該采用默認(rèn)設(shè)置,使用SQLite作為持久化存儲(chǔ)庫
*/

添加數(shù)據(jù):

// 傳入上下文,創(chuàng)建一個(gè)Person實(shí)體對(duì)象 
NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
// 設(shè)置簡單屬性 
[person setValue:@"JN" forKey:@"name"];
[person setValue:[NSNumber numberWithInt:22] forKey:@"age"];
// 傳入上下文,創(chuàng)建一個(gè)Card實(shí)體對(duì)象 
NSManagedObject *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:context];
[card setValue:@"447640819" forKey:@"no"];
// 設(shè)置Person和Card之間的關(guān)聯(lián)關(guān)系 
[person setValue:card forKey:@"card"];
 // 利用上下文對(duì)象,將數(shù)據(jù)同步到持久化存儲(chǔ)庫 
NSError *error = nil;BOOL success = [context save:&error];
if (!success) { 
    [NSException raise:@"訪問數(shù)據(jù)庫錯(cuò)誤" format:@"%@", [error localizedDescription]];
}
// 如果是想做更新操作:只要在更改了實(shí)體對(duì)象的屬性后調(diào)用[context save:&error],就能將更改的數(shù)據(jù)同步到數(shù)據(jù)庫

查詢數(shù)據(jù):

// 初始化一個(gè)查詢請(qǐng)求 
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 設(shè)置要查詢的實(shí)體 
NSEntityDescription *desc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
// 設(shè)置排序(按照age降序) 
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
request.sortDescriptors = [NSArray arrayWithObject:sort];
// 設(shè)置條件過濾(name like '%JN-1%') 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"*JN-1*"];
request.predicate = predicate;
注意:設(shè)置條件過濾時(shí),數(shù)據(jù)庫里面的%要用*來代替
// 執(zhí)行請(qǐng)求 
NSError *error = nil;
NSArray *objs = [context executeFetchRequest:request error:&error];
if (error) {
   [NSException raise:@"查詢錯(cuò)誤" format:@"%@", [error localizedDescription]];
}
// 遍歷數(shù)據(jù) 
for (NSManagedObject *obj in objs) {
   NSLog(@"name=%@", [obj valueForKey:@"name"]
}

刪除數(shù)據(jù):

// 傳入需要?jiǎng)h除的實(shí)體對(duì)象
 [context deleteObject:managedObject];
// 將結(jié)果同步到數(shù)據(jù)庫NSError *error = nil;
 [context save:&error];
if (error) { 
  [NSException raise:@"刪除錯(cuò)誤" format:@"%@",[error localizedDescription]];
}

Core Data的延遲加載:

Core Data不會(huì)根據(jù)實(shí)體中的關(guān)聯(lián)關(guān)系立即獲取相應(yīng)的關(guān)聯(lián)對(duì)象;比如通過Core Data取出Person實(shí)體時(shí),并不會(huì)立即查詢相關(guān)聯(lián)的Card實(shí)體,當(dāng)應(yīng)用真的需要使用Card時(shí),才會(huì)查詢數(shù)據(jù)庫,加載Card實(shí)體信息.

創(chuàng)建NSManagedObject的子類:

默認(rèn)情況下,利用Core Data取出的實(shí)體都是NSManagedObject類型的,能夠利用鍵-值對(duì)來存取數(shù)據(jù).

但是一般情況下,實(shí)體在存取數(shù)據(jù)的基礎(chǔ)上,有時(shí)還需要添加一些業(yè)務(wù)方法來完成一些其他任務(wù),那么就必須創(chuàng)建NSManagedObject的子類.

// 那么生成一個(gè)Person實(shí)體對(duì)象就應(yīng)該這樣寫 
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
person.name = @"JN";person.age = [NSNumber numberWithInt:24];
Card *card = [NSEntityDescription insertNewObjectForEntityForName:@”Card" inManagedObjectContext:context];
card.no = @”447640819";
person.card = card;

以上就是iOS中常用的應(yīng)用數(shù)據(jù)存儲(chǔ)方式及其詳細(xì)用法,本文分別介紹了XML屬性列表(plist)歸檔、Preference(偏好設(shè)置)、NSKeyedArchiver歸檔、SQLite3和Core Data,運(yùn)用了實(shí)例詳細(xì)介紹了實(shí)現(xiàn)過程及注意事項(xiàng)。希望本文對(duì)大家學(xué)習(xí)IOS開發(fā)能夠有所幫助。也請(qǐng)大家繼續(xù)支持腳本之家。

相關(guān)文章

最新評(píng)論