1.假如想創立一個帶有coreData的法式,要在項目初始化的時刻勾選中
2.創立完成以後,會發明在AppDelegate裡多出了幾個屬性,和2個辦法
<span >
@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,添加想要的字段
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.掏出數據查詢
/* 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 >
<span ><span ><br>
</span></span></p>
<span >留意</span><span >ascending:YES 屬性決議排序次序</span><span ><span ><br>
<br>
<br>
</span></span><br>
【詳解iOS運用開辟中Core Data數據存儲的應用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!