詳解iOS應(yīng)用開(kāi)發(fā)中Core Data數(shù)據(jù)存儲(chǔ)的使用
1.如果想創(chuàng)建一個(gè)帶有coreData的程序,要在項(xiàng)目初始化的時(shí)候勾選中
2.創(chuàng)建完成之后,會(huì)發(fā)現(xiàn)在AppDelegate里多出了幾個(gè)屬性,和2個(gè)方法
<span style="font-size:18px;">
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;</span>
Core Data數(shù)據(jù)持久化是對(duì)SQLite的一個(gè)升級(jí),它是ios集成的,在說(shuō)Core Data之前,我們先說(shuō)說(shuō)在CoreData中使用的幾個(gè)類。
(1)NSManagedObjectModel(被管理的對(duì)象模型)
相當(dāng)于實(shí)體,不過(guò)它包含 了實(shí)體間的關(guān)系
(2)NSManagedObjectContext(被管理的對(duì)象上下文)
操作實(shí)際內(nèi)容
作用:插入數(shù)據(jù) 查詢 更新 刪除
(3)NSPersistentStoreCoordinator(持久化存儲(chǔ)助理)
相當(dāng)于數(shù)據(jù)庫(kù)的連接器
(4)NSFetchRequest(獲取數(shù)據(jù)的請(qǐng)求)
相當(dāng)于查詢語(yǔ)句
(5)NSPredicate(相當(dāng)于查詢條件)
(6)NSEntityDescription(實(shí)體結(jié)構(gòu))
(7)后綴名為.xcdatamodel的包
里面的.xcdatamodel文件,用數(shù)據(jù)模型編輯器編輯
編譯后為.momd或.mom文件,這就是為什么文件中沒(méi)有這個(gè)東西,而我們的程序中用到這個(gè)東西而不會(huì)報(bào)錯(cuò)的原因。
3.如果想創(chuàng)建一個(gè)實(shí)體對(duì)象的話,需要點(diǎn)擊.xcdatamodel,Add Entity,添加想要的字段
4.生成對(duì)象文件,command+n,然后選中CoreData里的NSManagerObjectSubClass進(jìn)行關(guān)聯(lián),選中實(shí)體創(chuàng)建
5.添加數(shù)據(jù)
Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
if (newPerson == nil){
NSLog(@"Failed to create the new person.");
return NO;
}
newPerson.firstName = paramFirstName;
newPerson.lastName = paramLastName;
newPerson.age = [NSNumber numberWithUnsignedInteger:paramAge];
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
return YES;
} else {
NSLog(@"Failed to save the new person. Error = %@", savingError);
}
NSEntityDescription(實(shí)體結(jié)構(gòu))相當(dāng)于表格結(jié)構(gòu)
6.取出數(shù)據(jù)查詢
/* Create the fetch request first */
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
/* Here is the entity whose contents we want to read */
NSEntityDescription *entity =
[NSEntityDescription
entityForName:@"Person"
inManagedObjectContext:self.managedObjectContext];
/* Tell the request that we want to read the
contents of the Person entity */
[fetchRequest setEntity:entity];
NSError *requestError = nil;
/* And execute the fetch request on the context */
NSArray *persons =
[self.managedObjectContext executeFetchRequest:fetchRequest
error:&requestError];
/* Make sure we get the array */
if ([persons count] > 0){
/* Go through the persons array one by one */
NSUInteger counter = 1;
for (Person *thisPerson in persons){
NSLog(@"Person %lu First Name = %@",
(unsigned long)counter,
thisPerson.firstName);
NSLog(@"Person %lu Last Name = %@",
(unsigned long)counter,
thisPerson.lastName);
NSLog(@"Person %lu Age = %ld",
(unsigned long)counter,
(unsigned long)[thisPerson.age unsignedIntegerValue]);
counter++;
}
} else {
NSLog(@"Could not find any Person entities in the context.");
}
7.刪除數(shù)據(jù)
/* Create the fetch request first */
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
/* Here is the entity whose contents we want to read */
NSEntityDescription *entity =
[NSEntityDescription
entityForName:@"Person"
inManagedObjectContext:self.managedObjectContext];
/* Tell the request that we want to read the
contents of the Person entity */
[fetchRequest setEntity:entity];
NSError *requestError = nil;
/* And execute the fetch request on the context */
NSArray *persons =
[self.managedObjectContext executeFetchRequest:fetchRequest
error:&requestError];
if ([persons count] > 0){
/* Delete the last person in the array */
Person *lastPerson = [persons lastObject];
[self.managedObjectContext deleteObject:lastPerson];
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
NSLog(@"Successfully deleted the last person in the array.");
} else {
NSLog(@"Failed to delete the last person in the array.");
}
} else {
NSLog(@"Could not find any Person entities in the context.");
}
8.排序
<pre code_snippet_id="243955" snippet_file_name="blog_20140319_5_4289257" name="code" class="objc">NSSortDescriptor *ageSort =
[[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES];
NSSortDescriptor *firstNameSort =
[[NSSortDescriptor alloc] initWithKey:@"firstName"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:
ageSort,
firstNameSort, nil nil];
fetchRequest.sortDescriptors = sortDescriptors; </pre><p></p>
<pre></pre>
<p></p>
<p style="background-color:rgb(255,255,255); margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,'ms song',Arial,Helvetica,sans-serif; line-height:19.09090805053711px">
<span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br>
</span></span></p>
<span style="background-color:rgb(255,255,255)">注意</span><span style="font-size:18px; background-color:rgb(255,255,255)">ascending:YES 屬性決定排序順序</span><span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br>
<br>
<br>
</span></span><br>
- 詳解iOS的數(shù)據(jù)存儲(chǔ)
- 詳解iOS App開(kāi)發(fā)中session和coockie的用戶數(shù)據(jù)存儲(chǔ)處理
- iOS內(nèi)存錯(cuò)誤EXC_BAD_ACCESS的解決方法
- iOS毛玻璃效果的實(shí)現(xiàn)及圖片模糊效果的三種方法
- IOS獲取各種文件目錄路徑的方法
- 如何用IOS調(diào)用WebService(SOAP接口)
- IOS開(kāi)發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
- iOS中使用schema協(xié)議調(diào)用APP和使用iframe打開(kāi)APP的例子
- iOS開(kāi)發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
- IOS 數(shù)據(jù)存儲(chǔ)詳解及實(shí)例代碼
相關(guān)文章
淺談iOS開(kāi)發(fā)中static變量的三大作用
下面小編就為大家?guī)?lái)一篇淺談iOS開(kāi)發(fā)中static變量的三大作用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03詳解IOS開(kāi)發(fā)中圖片上傳時(shí)兩種圖片壓縮方式的比較
這篇文章主要介紹了IOS開(kāi)發(fā)中圖片上傳時(shí)兩種圖片壓縮方式的比較,需要的朋友可以參考下2017-03-03iOS實(shí)現(xiàn)應(yīng)用內(nèi)切換本地化語(yǔ)言的方法實(shí)例
網(wǎng)絡(luò)上關(guān)于iOS國(guó)際化的文章很多,但基本上都是基于跟隨系統(tǒng)語(yǔ)言的國(guó)際化,而這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)應(yīng)用內(nèi)切換本地化語(yǔ)言的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考。2017-12-12iOS下PDF文件的瀏覽和涂鴉效果的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了iOS下PDF文件的瀏覽和涂鴉效果的簡(jiǎn)單實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10iOS 設(shè)置導(dǎo)航條透明效果的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了ios 設(shè)置導(dǎo)航條透明效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03IOS實(shí)現(xiàn)碎片化動(dòng)畫(huà)詳解
在網(wǎng)上看到一個(gè)驚艷的碎片化動(dòng)畫(huà),于是實(shí)現(xiàn)之后拿來(lái)講解一下,有需要的小伙伴們可以參考學(xué)習(xí)哦。2016-08-08實(shí)例講解iOS應(yīng)用的設(shè)計(jì)模式開(kāi)發(fā)中的Visitor訪問(wèn)者模式
這篇文章主要介紹了iOS應(yīng)用的設(shè)計(jì)模式開(kāi)發(fā)中的Visitor訪問(wèn)者模式的實(shí)例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS10添加本地推送(Local Notification)實(shí)例
這篇文章主要為大家詳細(xì)介紹了iOS10添加本地推送(Local Notification)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09