- 表格結構:NSEntityDescription 相當於數據庫中的一個表,TA描述一種抽象數據類型
- eg:
- //+insertNewObjectForEntityForName:inManagedObjectContext: 工廠方法,根據給定的 Entity 描述,生成相應的 NSManagedObject 對象,並插入到 ManagedObjectContext 中
- Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:managedObjectContext];
- //通過上面的代碼可以得到student這個表的實例,然後可以使用這個實例去爲表中的屬性賦值
- student.name = @"like"; student.age = 12;
- 應用程序的數據模型,數據庫中所有表格和他們之間的聯繫:NSManagedObjectModel
- //系統會讀取model文件來聲稱NSManagedObjectModel對象,
- model :對於class,model稱之爲 entity; 對於instant variable, model 稱之爲property
- model包含兩種property: attributes 和 relationships. attribute爲簡單數據類型,如一個字符串,日期,數字
- NSManagedObjectModel * model = [self managedObjectModel];//獲取實例
- NSDictionary * entities = [model entitiesByName];//entitiesByName 得到所有的表的名字
- NSEntityDescription * entity = [entities valueForKey:@"Student"];//從裏面找出名爲 Student 的表
- 數據庫存放方式:NSPersistentStoreCoordinator
- //使用 Core Data document 類型的應用程序,通常會從磁盤上的數據文中中讀取或存儲數據,這寫底層的讀寫就由 Persistent Store Coordinator 來處理。一般我們無需與它直接打交道來讀寫文件,Managed Object Context 在背後已經爲我們調用 Persistent Store Coordinator 做了這部分工作
- 數據庫操作:NSManagedObjectContext 被管理的對象上下文(對數據直接操作)
- //可以通過TA去訪問底層的框架對象集合,這些對象集合統稱爲持久化堆棧(persistence stack)——它在應用程序和外部數據存儲的對象之間提供訪問通道
- //Managed Object Context 的作用相當重要,對數據對象進行的操作都與它有關。當創建一個數據對象並插入 Managed Object Context 中,Managed Object Context 就開始跟蹤這個數據對象的一切變動,並在合適的時候提供對 undo/redo 的支持,或調用 Persistent Store Coordinato 將變化保存到數據文件中去
- //redo: 恢復已經提交的事務 undo: 回滾操作,支持讀一致性,恢復失敗的事務
- //-executeFetchRequest: error: 執行 Fetch Request 並返回所有匹配的數據對象
- NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
- NSArray * results = [context executeFetchRequest:fetch error:nil];
- //獲取NSManagedObjectContext的實例
- //1.利用UIApplication代理獲取
- id delegate = [[UIApplication sharedApplication] delegate];
- self.managedObjectContext = [delegate managedObjectContext];
- //2.直接去創建實例
- NSManagedObjectContext *context = [self managedObjectContext];//利用getter方法來獲取
- 查詢語句:NSPetchRequest 相當於select語句
- //Fetch Requests 相當於一個查詢語句,你必須指定要查詢的 Entity。我們通過 Fetch Requests 向 Managed Object Context 查詢符合條件的數據對象,以 NSArray 形式返回查詢結果,如果我們沒有設置任何查詢條件,則返回該 Entity 的所有數據對象。我們可以使用謂詞來設置查詢條件,通常會將常用的 Fetch Requests 保存到 dictionary 以重複利用
- NSPredicate * predicate = [NSPredicate predicateWithFormat:@"xx > %@", xx];//相當於select中的查詢條件
- NSSortDescriptor * sort = [[NSortDescriptor alloc] initWithKey:@"name"];//按照 name 來排序
- NSArray * sortDescriptors = [NSArray arrayWithObject: sort];
- // -setEntity: 設置你要查詢的數據對象的類型(Entity)就是NSEntityDescription對象
- // -setPredicate: 設置查詢條件
- // -setFetchLimit: 設置最大查詢對象數目
- // -setSortDescriptors: 設置查詢結果的排序方法
- // -setAffectedStores: 設置可以在哪些數據存儲中查詢
- 表格的記錄:NSManagedObject Core Data存儲的每個對象都繼承自NSManagedObject,我們可以得到TA的一個實例,用這個實例去爲表中的屬性賦值
- eg:
- NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:managedObjectContext];
- managedObject.name = @"like"; managedObject.age = 12;
IOS成長之路-Core Data使用時用到的類--微解
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.