除了使用SQL關(guān)系數(shù)據(jù)庫外,我們還可以使用Xcode中提供的核心數(shù)據(jù)來處理表結(jié)構(gòu)數(shù)據(jù),今天就讓武林技術(shù)頻道帶大家一起進入下文了解詳解iOS應用開發(fā)中Core Data數(shù)據(jù)存儲的使用吧,希望對你學習有幫助!
1.如果想創(chuàng)建一個帶有coreData的程序,要在項目初始化的時候勾選中
?
2.創(chuàng)建完成之后,會發(fā)現(xiàn)在AppDelegate里多出了幾個屬性,和2個方法
?
?
?
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;?
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;?
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;?
?
- (void)saveContext;?
- (NSURL *)applicationDocumentsDirectory;?
Core Data數(shù)據(jù)持久化是對SQLite的一個升級,它是ios集成的,在說Core Data之前,我們先說說在CoreData中使用的幾個類。
?
(1)NSManagedObjectModel(被管理的對象模型)
相當于實體,不過它包含 了實體間的關(guān)系
(2)NSManagedObjectContext(被管理的對象上下文)
操作實際內(nèi)容
作用:插入數(shù)據(jù)? 查詢? 更新? 刪除
(3)NSPersistentStoreCoordinator(持久化存儲助理)
相當于數(shù)據(jù)庫的連接器
(4)NSFetchRequest(獲取數(shù)據(jù)的請求)
相當于查詢語句
(5)NSPredicate(相當于查詢條件)
(6)NSEntityDescription(實體結(jié)構(gòu))
(7)后綴名為.xcdatamodel的包
里面的.xcdatamodel文件,用數(shù)據(jù)模型編輯器編輯
編譯后為.momd或.mom文件,這就是為什么文件中沒有這個東西,而我們的程序中用到這個東西而不會報錯的原因。
3.如果想創(chuàng)建一個實體對象的話,需要點擊.xcdatamodel,Add Entity,添加想要的字段


4.生成對象文件,command+n,然后選中CoreData里的NSManagerObjectSubClass進行關(guān)聯(lián),選中實體創(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(實體結(jié)構(gòu))相當于表格結(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.排序
?
?
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;
?
?
?
?
?
?
?
?
注意ascending:YES 屬性決定排序順序?
?
? ?
通過武林技術(shù)頻道小編介紹的內(nèi)容,相信大家都有了一定的了解,想要了解更多的技術(shù)內(nèi)容,請繼續(xù)關(guān)注武林技術(shù)頻道吧!