码迷,mamicode.com
首页 > 其他好文 > 详细

CoreData的基础知识

时间:2015-11-16 20:55:35      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

1.CoreData的专业术语

  NSManagerModel   被管理的数据模型
  NSManngerObject   被管理的数据对象
  NSPersistentStoreCoordinator   持久化存储助理
  NSManagerContext   被管理的数据的上下文
  NSEntityDspcipition   实体结构(相当于表格结构)
  NSFetchRequest   获得数据的请求
  NSPredicate   谓词(筛选数据)
  后缀为.xcdatamodeld的包   编译后为.momd或.mom文件

 

 

 

 

 

 

 

 

2.手动创建CoreData的步骤:

 (1).在target找到build phases中link binary with libraries条目。添加coredata.framework。

 (2).新建一个new file,在coredata中找到data model,建立。

 (3).在appdelegate.h中引入:

1 @property (readonly,strong,nonatomic)NSManagedObjectContext *managedObjectContext;
2 @property (readonly,strong,nonatomic)NSManagedObjectModel *managedObjectModel;
3 @property (readonly,strong,nonatomic)NSPersistentStoreCoordinator *persistentStoreCoordinator;

在appdelegate.m中初始化三个对象:

 1 #pragma mark - coradata
 2 @synthesize managedObjectContext =_managedObjectContext;
 3 @synthesize managedObjectModel = _managedObjectModel;
 4 @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
 5 
 6 - (NSURL *)applicationDocumentsDirectory {
 7     // The directory the application uses to store the Core Data store file. This code uses a directory named "moxue.test111" in the application‘s documents directory.
 8     return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
 9 }
10 - (NSManagedObjectModel *)managedObjectModel {
11     // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
12     if (_managedObjectModel != nil) {
13         return _managedObjectModel;
14     }
15     NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
16     _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
17     return _managedObjectModel;
18 }
19 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
20     // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
21     if (_persistentStoreCoordinator != nil) {
22         return _persistentStoreCoordinator;
23     }
24     
25     // Create the coordinator and store
26     
27     _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
28     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Model.sqlite"];
29     NSError *error = nil;
30     NSString *failureReason = @"There was an error creating or loading the application‘s saved data.";
31     if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
32         // Report any error we got.
33         NSMutableDictionary *dict = [NSMutableDictionary dictionary];
34         dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application‘s saved data";
35         dict[NSLocalizedFailureReasonErrorKey] = failureReason;
36         dict[NSUnderlyingErrorKey] = error;
37         error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
38         // Replace this with code to handle the error appropriately.
39         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
40         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
41         abort();
42     }
43     
44     return _persistentStoreCoordinator;
45 }
46 - (NSManagedObjectContext *)managedObjectContext {
47     // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
48     if (_managedObjectContext != nil) {
49         return _managedObjectContext;
50     }
51     
52     NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
53     if (!coordinator) {
54         return nil;
55     }
56     _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
57     [_managedObjectContext setPersistentStoreCoordinator:coordinator];
58     return _managedObjectContext;
59 }
60 #pragma mark - Core Data Saving support
61 
62 - (void)saveContext
63 {
64     NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
65     if (managedObjectContext != nil) {
66         NSError *error = nil;
67         if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
68             // Replace this implementation with code to handle the error appropriately.
69             // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
70             NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
71             abort();
72         }
73     }
74 }
75 @end

 注意:(红字标记处要和你建立的.xcdatamodeld文件的数据模型名称相对应)。

 到此就可以在xcdatamodeld文件中编译了。

3.插入数据

 

CoreData的基础知识

标签:

原文地址:http://www.cnblogs.com/moxuexiaotong/p/4850818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!