1.如果想創建一個帶有coreData的程序,要在項目初始化的時候勾選中
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>
managedObjectContext (被管理的數據上下文)操作實際內容(操作持久層)作用:插入數據,查詢數據,刪除數據
NSManagedObjectModel(被管理的數據模型)數據庫所有表格或數據結構,包含各實體的定義信息 作用:添加實體的屬性,建立屬性之間的關系操作方法:視圖編輯器,或代碼
NSPersistentStoreCoordinator(持久化存儲助理)相當于數據庫的連接器 作用:設置數據存儲的名字,位置,存儲方式,和存儲時機
方法saveContext表示:保存數據到持久層(數據庫)
方法applicationDocumentsDirectory表示:應用程序沙箱下的Documents目錄路徑
3.如果想創建一個實體對象的話,需要點擊.xcdatamodel,Add Entity,添加想要的字段


4.生成對象文件,command+n,然后選中CoreData里的NSManagerObjectSubClass進行關聯,選中實體創建
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.取出數據查詢
-
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
-
- NSEntityDescription *entity =
- [NSEntityDescription
- entityForName:@"Person"
- inManagedObjectContext:self.managedObjectContext];
-
-
- [fetchRequest setEntity:entity];
- NSError *requestError = nil;
-
- NSArray *persons =
- [self.managedObjectContext executeFetchRequest:fetchRequest
- error:&requestError];
-
- if ([persons count] > 0){
-
- 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.刪除數據
-
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
-
- NSEntityDescription *entity =
- [NSEntityDescription
- entityForName:@"Person"
- inManagedObjectContext:self.managedObjectContext];
-
-
- [fetchRequest setEntity:entity];
- NSError *requestError = nil;
-
- NSArray *persons =
- [self.managedObjectContext executeFetchRequest:fetchRequest
- error:&requestError];
- if ([persons count] > 0){
-
- 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>