国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

詳解iOS應用開發中Core Data數據存儲的使用

2019-10-21 18:55:58
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS應用開發中Core Data數據存儲的使用,Core Data可以看作是一個內嵌型數據庫SQLite的iOS專用版本,需要的朋友可以參考下
 

1.如果想創建一個帶有coreData的程序,要在項目初始化的時候勾選中
詳解iOS應用開發中Core Data數據存儲的使用 
2.創建完成之后,會發現在AppDelegate里多出了幾個屬性,和2個方法 

復制代碼代碼如下:

<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數據持久化是對SQLite的一個升級,它是ios集成的,在說Core Data之前,我們先說說在CoreData中使用的幾個類。

 

(1)NSManagedObjectModel(被管理的對象模型)

相當于實體,不過它包含 了實體間的關系

(2)NSManagedObjectContext(被管理的對象上下文)

操作實際內容

作用:插入數據  查詢  更新  刪除

(3)NSPersistentStoreCoordinator(持久化存儲助理)

相當于數據庫的連接器

(4)NSFetchRequest(獲取數據的請求)

相當于查詢語句

(5)NSPredicate(相當于查詢條件)

(6)NSEntityDescription(實體結構)

(7)后綴名為.xcdatamodel的包

里面的.xcdatamodel文件,用數據模型編輯器編輯

編譯后為.momd或.mom文件,這就是為什么文件中沒有這個東西,而我們的程序中用到這個東西而不會報錯的原因。


3.如果想創建一個實體對象的話,需要點擊.xcdatamodel,Add Entity,添加想要的字段

詳解iOS應用開發中Core Data數據存儲的使用詳解iOS應用開發中Core Data數據存儲的使用

4.生成對象文件,command+n,然后選中CoreData里的NSManagerObjectSubClass進行關聯,選中實體創建

詳解iOS應用開發中Core Data數據存儲的使用

5.添加數據 

復制代碼代碼如下:

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(實體結構)相當于表格結構

6.取出數據查詢 

復制代碼代碼如下:

/* 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.刪除數據 

復制代碼代碼如下:

/* 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 稻城县| 商城县| 泰和县| 凤台县| 文安县| 丘北县| 昌图县| 纳雍县| 广宁县| 甘德县| 会理县| 青阳县| 大邑县| 青川县| 长泰县| 尚志市| 苗栗县| 建始县| 马鞍山市| 吐鲁番市| 宁乡县| 安图县| 韶关市| 麟游县| 阿城市| 河津市| 永吉县| 孙吴县| 鄂托克前旗| 汤阴县| 平定县| 杭锦旗| 岑溪市| 郓城县| 乌鲁木齐市| 尼玛县| 克山县| 克山县| 嵊州市| 五大连池市| 潜山县|