在xcode 4.2.1中通过RHManagedObject 和RHManagedObjectContextManager 来简化 CoreData的处理
2011-12-01 15:57
531 查看
项目源码下载 : 在我的资源里面。
RHManagedObjectContextManager 和 RHManagedObject 来自与 https://github.com/chriscdn/RHManagedObject 在项目中加入 CoreData.framework的框架
在项目中 加入 RHManagedObjectContextManager.h 和 RHManagedObjectContextManager.m ,RHManagedObject.h ,RHManagedObject.m 4个文件。
通过 xcode 4.2 新建一个文件,文件类型是 CoreData 下的Data Model 。命名为
test.xcdatamodeld
然后点击 这个 test.xcdatamodeld 文件,在主视图里面 加入一个 Entity ,名字 叫 :Customer。增加2个 Attribute :1 : name ,2 age
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/14/256eb8b118fc0b09bab0a8d3c431aa91.gif)
点击其他文件,取消选中这个 test.xcdatamodeld 文件
新建一个文件,类型是CoreData下的NSManagedObject subclass
然后界面上会显示 Data Model的名称,这里显示 test。选中这个test,点击next按钮 ,出现这个 test.xcdatamodeld 文件中定义的Entities ,这里显示 Customer.选中这个Customer ,点击next 按钮。然后在选择保存文档的目录。按按钮 create。这样在项目中就会建立 Customer.h 和 Customer.m文件。
修改 Customer.h文件
#import "RHManagedObject.h"
把原来的基类由 NSManagedObject 修改 为 RHManagedObject. @interface Customer : RHManagedObject
修改RHManagedObjectContextManager.h,这只 2个 define值
#define kDatabaseName @"test.sqlite"
#define kModelName @"test"
kDatabaseName指定的是sqlite数据库的名称,可以任意命名,生成后文件保存在Documents目录下
kModelName指的是数据模型的名称,必须和在项目中创建的.xcdatamodeld文件的文件名前缀名称一致.
修改 Customer.m 的源码 ,加入以下代码:
编写数据库 访问功能
官网的例子:
Add
Fetch
Fetch
Fetch
Get
The
the first object if more than one is found.
Count
Count
Get
Get
Fire
Fire
Commit
This must be called in the same thread where the changes to your objects were made.
Completely
This is useful to reset your Core Data store after making changes to your model.
Get
Core Data doesn't allow managed objects to be passed between threads. However you can generate a new object in a separate thread that's valid for that thread. Here's an example using the
RHManagedObjectContextManager 和 RHManagedObject 来自与 https://github.com/chriscdn/RHManagedObject 在项目中加入 CoreData.framework的框架
在项目中 加入 RHManagedObjectContextManager.h 和 RHManagedObjectContextManager.m ,RHManagedObject.h ,RHManagedObject.m 4个文件。
通过 xcode 4.2 新建一个文件,文件类型是 CoreData 下的Data Model 。命名为
test.xcdatamodeld
然后点击 这个 test.xcdatamodeld 文件,在主视图里面 加入一个 Entity ,名字 叫 :Customer。增加2个 Attribute :1 : name ,2 age
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/14/256eb8b118fc0b09bab0a8d3c431aa91.gif)
点击其他文件,取消选中这个 test.xcdatamodeld 文件
新建一个文件,类型是CoreData下的NSManagedObject subclass
然后界面上会显示 Data Model的名称,这里显示 test。选中这个test,点击next按钮 ,出现这个 test.xcdatamodeld 文件中定义的Entities ,这里显示 Customer.选中这个Customer ,点击next 按钮。然后在选择保存文档的目录。按按钮 create。这样在项目中就会建立 Customer.h 和 Customer.m文件。
修改 Customer.h文件
#import "RHManagedObject.h"
把原来的基类由 NSManagedObject 修改 为 RHManagedObject. @interface Customer : RHManagedObject
修改RHManagedObjectContextManager.h,这只 2个 define值
#define kDatabaseName @"test.sqlite"
#define kModelName @"test"
kDatabaseName指定的是sqlite数据库的名称,可以任意命名,生成后文件保存在Documents目录下
kModelName指的是数据模型的名称,必须和在项目中创建的.xcdatamodeld文件的文件名前缀名称一致.
修改 Customer.m 的源码 ,加入以下代码:
+(NSString *)entityName { return @"Customer"; } // This returns the name of your xcdatamodeld model, without the extension+(NSString *)modelName { return @"test"; // 这里要放入xcdatamodeld文件名。不需要后缀}
编写数据库 访问功能
//****************** 数据保存 begin Customer * customer = (Customer *) [Customer newEntity]; customer.name =@"asdfsf"; customer.age = [[ NSNumber alloc] initWithFloat: 13.4f]; [RHManagedObject commit]; //****************** 数据保存 end
-(NSArray * ) allRecords { NSLog(@"获取所有记录"); //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1=1"]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"smount" ascending:YES]; return [[NSArray alloc] initWithArray:[CustomerInfo fetchWithSortDescriptor:sortDescriptor]]; }
官网的例子:
Examples
Once you have setup RHManagedObject it becomes easier to do common tasks. Here are some examples.Add
a new employee
Employee *employee = [Employee newEntity]; [employee setFirstName:@"John"]; [employee setLastName:@"Doe"];
Fetch
all employees
NSArray *allEmployees = [Employee fetchAll];
Fetch
all employees with first name "John"
NSArray *employees = [Employee fetchWithPredicate:[NSPredicate predicateWithFormat:@"firstName=%@", @"John"]];
Fetch
all employees with first name "John" sorted by last name
NSArray *employees = [Employee fetchWithPredicate:[NSPredicate predicateWithFormat:@"firstName=%@", @"John"] sortDescriptor:[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]];
Get
a specific employee record
The +getWithPredicate:method will return
the first object if more than one is found.
Employee *employee = [Employee getWithPredicate:[NSPredicate predicateWithFormat:@"employeeID=%i", 12345]];
Count
the total number of employees
NSUInteger employeeCount = [Employee count];
Count
the total number of employees with first name "John"
NSUInteger employeeCount = [Employee countWithPredicate:[NSPredicate predicateWithFormat:@"firstName=%@", @"John"]];
Get
all the unique first names
NSArray *uniqueFirstNames = [Employee distinctValuesWithAttribute:@"firstName" withPredicate:nil];
Get
the average age of all employees
NSNumber *averageAge = [Employee aggregateWithType:RHAggregateAverage key:@"age" predicate:nil defaultValue:nil];
Fire
all employees
[Employee deleteAll];
Fire
a single employee
Employee *employee = [Employee get ...]; [employee delete];
Commit
changes
This must be called in the same thread where the changes to your objects were made.[Employee commit];
Completely
destroy the Employee model (i.e., delete the .sqlite file)
This is useful to reset your Core Data store after making changes to your model.[Employee deleteStore];
Get
an object instance in another thread
Core Data doesn't allow managed objects to be passed between threads. However you can generate a new object in a separate thread that's valid for that thread. Here's an example using the -objectInCurrentThreadContextmethod:
Employee *employee = [Employee getWithPredicate:[NSPredicate predicateWithFormat:@"employeID=%i", 12345]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// employee is not valid in this thread, so we fetch one that is:
Employee *employee2 = [employee objectInCurrentThreadContext];
// do something with employee2
[Employee commit];});
相关文章推荐
- 使用RHManagedObjectContextManager 来进行 CoreData的数据处理
- CoreData 多线程下NSManagedObjectContext的使用
- iphone:Core Data:Where does a Managed Object Context Come From?
- CoreData 多线程下NSManagedObjectContext的使用
- Core Data:Receiver type ‘NSManagedObjectContext’ for class is a forward declaration
- 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误
- Core Data:Receiver type ‘NSManagedObjectContext’ for class is a forward declaration
- 002_你实现过多线程的Core Data么?NSPersistentStoreCoordinator,NSManagedObjectContext和NSManagedObject中的哪些需要在线程中
- Multi-Context CoreData
- ava EE 7 - Injection into Runnable/Callable object ejb entityManager Concurrency ManagedExecutorService 异步调用如何获取context
- Object-C系列-<深入CoreData与问题整理>
- Xcode利用CoreData编成简易内置通讯录
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[中]:管道如何处理请求
- MagicalRecord,一个简化CoreData操作的工具库
- Core J2EE Patterns - Data Access Object
- MagicalRecord(简化CoreData操作)
- Method not found : Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReference...
- 在使用EFCodeFirst中出现类型“System.Data.Objects.ObjectContext”在未被引用的程序集中定义的解决方案
- System.Data.Objects.ObjectContext 的 49个方法、7个属性、4个构造函数和2个事件(转)
- CoreData 数据传输对象DTO(data transfer object)