您好,登录后才能下订单哦!
1、NSManagedObjectModel:管理的对象模型,相当于实体,不过它包含了实体间的关系
2、NSManagedObjectContext:管理的对象上下文,操作实际内容,作用:插入数据 查询 更新 删除
3、NSPersistentStoreCoordinator:持久化存储助理,相当于数据库的连接器
4、NSFetchRequest:获取数据的请求,相当于查询语句
5、NSPredicate:相当于查询条件
6、NSEntityDescription:实体结构
7、后缀名为.xcdatamodel的包,里面的.xcdatamodel文件,用数据模型编辑器编辑,编译后为.MOMD或.MOM文件;
操作步骤:
一、建立模型对象
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@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
//托管对象
-(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;
}
//持久化存储协调器
-(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;
}
//托管对象上下文
-(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]:返回指定位置的数据.
[fetchedResultsController sections]:获取section数据,返回的是NSFetchedResultsSectionInfo数据
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。