1、NSManagedObjectModel:管理的對(duì)象模型,相當(dāng)于實(shí)體,不過(guò)它包含了實(shí)體間的關(guān)系
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括壺關(guān)網(wǎng)站建設(shè)、壺關(guān)網(wǎng)站制作、壺關(guān)網(wǎng)頁(yè)制作以及壺關(guān)網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,壺關(guān)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到壺關(guān)省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
2、NSManagedObjectContext:管理的對(duì)象上下文,操作實(shí)際內(nèi)容,作用:插入數(shù)據(jù) 查詢 更新 刪除
3、NSPersistentStoreCoordinator:持久化存儲(chǔ)助理,相當(dāng)于數(shù)據(jù)庫(kù)的連接器
4、NSFetchRequest:獲取數(shù)據(jù)的請(qǐng)求,相當(dāng)于查詢語(yǔ)句
5、NSPredicate:相當(dāng)于查詢條件
6、NSEntityDescription:實(shí)體結(jié)構(gòu)
7、后綴名為.xcdatamodel的包,里面的.xcdatamodel文件,用數(shù)據(jù)模型編輯器編輯,編譯后為.MOMD或.MOM文件;
操作步驟:
一、建立模型對(duì)象
#import
#import
@class ViewController;
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property(strong,nonatomic,readonly)NSManagedObjectModel* managedObjectModel;
@property(strong,nonatomic,readonly)NSManagedObjectContext* managedObjectContext;
@property(strong,nonatomic,readonly)NSPersistentStoreCoordinator* persistentStoreCoordinator;
@end
//托管對(duì)象
-(NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel!=nil) {
return _managedObjectModel;
}
// NSURL* modelURL=[[NSBundle mainBundle] URLForResource:@"CoreData" withExtension:@"momd"];
// _managedObjectModel=[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
_managedObjectModel=[[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return _managedObjectModel;
}
//持久化存儲(chǔ)協(xié)調(diào)器
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator!=nil) {
return _persistentStoreCoordinator;
}
// NSURL* storeURL=[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreaData.CDBStore"];
// NSFileManager* fileManager=[NSFileManager defaultManager];
// if(![fileManager fileExistsAtPath:[storeURL path]])
// {
// NSURL* defaultStoreURL=[[NSBundle mainBundle] URLForResource:@"CoreData" withExtension:@"CDBStore"];
// if (defaultStoreURL) {
// [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
// }
// }
NSString* docs=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSURL* storeURL=[NSURL fileURLWithPath:[docs stringByAppendingPathComponent:@"CoreData.sqlite"]];
NSLog(@"path is %@",storeURL);
NSError* error=nil;
_persistentStoreCoordinator=[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Error: %@,%@",error,[error userInfo]);
}
return _persistentStoreCoordinator;
}
//托管對(duì)象上下文
-(NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext!=nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator* coordinator=[self persistentStoreCoordinator];
if (coordinator!=nil) {
_managedObjectContext=[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
-(NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end
[fetchedResultsController objectAtIndexPath]:返回指定位置的數(shù)據(jù).
[fetchedResultsController sections]:獲取section數(shù)據(jù),返回的是NSFetchedResultsSectionInfo數(shù)據(jù)