IOS成長之路-Core Data使用時用到的類--微解

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