数据持久化(六)之Using CoreData with MagicalRecord
第五节里面,我介绍了CoreData的配置和主要的增删改查,可能非常多人会认为用它真繁琐.这里,我再介绍网上大神对它进行了人性化封装的第三方MagicalRecord,正如FMDB对sqlite进行了封装一样,MagicalRecord让你认为用CoreData非常方便.
@基本配置:
1.下载MagicalRecord,将里面的MagicalRecord目录拖入你的project
2.确定你创建的project没有勾选"Use Core Data"
3.导入CoreData.frame框架
4.在.pch文件里引入头文件"CoreData+MagicalRecord.h"(仅仅能,必须在这里引用)
@详细操作:
1.初始化(在didFinishLaunchingWithOptions中)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; HMTRootViewController *rootVC = [[HMTRootViewController alloc] init];
self.window.rootViewController = rootVC; // 初始化
[MagicalRecord setupCoreDataStackWithStoreNamed:@"MT.sqlite"]; [self.window makeKeyAndVisible];
return YES;
} - (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[MagicalRecord cleanUp];
}
2.创建一个Model做測试,创建一个Person的Entity
3.增删改查详细操作
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. // 初始化一个Person对象
/**
* 这里要注意:默认,xcdatamodeld实体描写叙述表名(name)和类名(class)必须保持一致
* 假设name和class不一致,实现MagicalRecord_MOGenerator协议中得entityName方法来改变
@implementation NSManagedObject (MagicalRecord) + (NSString *) MR_entityName;
{
NSString *entityName; if ([self respondsToSelector:@selector(entityName)])
{
entityName = [self performSelector:@selector(entityName)];
} if ([entityName length] == 0) {
entityName = NSStringFromClass(self);
} return entityName;
}
*/
Person *person = [Person MR_createEntity];
person.name = @"HMT";
person.sex = @"男";
person.age = @25; } // 加入操作
- (void)addDataOperation{ // 文档解释:For any entities to actually be saved / updated / deleted on disk call following method(添加,更新,删除 都要用这种方法来保存数据)
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];;
} // 查询操作
- (void)selectDataOperation{ // find数据库中全部的人
NSArray *peoples = [Person MR_findAll];
// find数据库中第一条记录
Person *firPerson = [Person MR_findFirst];
// find数据库中全部name属性为"HMT"的人并依照年龄age排序
NSArray *otherPeoples = [Person MR_findByAttribute:@"name" withValue:@"HMT" andOrderBy:@"age" ascending:YES]; } // 更新操作
- (void)upDataOperation{ // 选定要改动的人
NSArray *persons = [Person MR_findByAttribute:@"name" withValue:@"HMT"];
for (Person *person in persons) {
person.name = @"WDQ";
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; } // 删除操作
- (void)deleteDataOperation{ // delete数据库中全部人
[Person MR_truncateAll];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; // 依据条件delete特定的某个人
NSArray *persons = [Person MR_findByAttribute:@"name" withValue:@"HMT"];
for (Person *person in persons) {
[person MR_deleteEntity];
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; }
4.额外配置(取消前缀名和不打印日志信息)
//If you want to omit the "MR_" prefix to all MagicalRecord realted method calls define following before importing the MagicalRecord header
#define MR_SHORTHAND //Turn off MagicalRecord logging, again by defining following before header import
#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
@以上仅仅是一些基本操作,其它能够自己去查看头文件
数据持久化(六)之Using CoreData with MagicalRecord的更多相关文章
- [Xcode 实际操作]七、文件与数据-(14)数据持久化存储框架CoreData的使用:删除CoreData中的数据
目录:[Swift]Xcode实际操作 本文将演示如何删除数据持久化对象. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入数据持 ...
- [Xcode 实际操作]七、文件与数据-(13)数据持久化存储框架CoreData的使用:编辑CoreData中的数据
目录:[Swift]Xcode实际操作 本文将演示如何修改数据持久化对象. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入数据持 ...
- [Xcode 实际操作]七、文件与数据-(12)数据持久化存储框架CoreData的使用:查找CoreData中的数据
目录:[Swift]Xcode实际操作 本文将演示如何查找数据持久化对象. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入数据持 ...
- [Xcode 实际操作]七、文件与数据-(11)数据持久化存储框架CoreData的使用:创建CoreData实体并插入数据
目录:[Swift]Xcode实际操作 本文将演示[CoreData]数据持久化存储框架的使用. 点击[Create a new Xcode project]创建一个新的项目 ->[Single ...
- IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)
IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...
- ios数据持久化--CoreData框架的介绍和使用
1.Core Data 是数据持久化存储的最佳方式 2.数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型 在Mac OS X 10.5Leopard及以后的版本中 ...
- iPhone开发 数据持久化总结(终结篇)—5种数据持久化方法对比
iPhone开发 数据持久化总结(终结篇)—5种数据持久化方法对比 iphoneiPhoneIPhoneIPHONEIphone数据持久化 对比总结 本篇对IOS中常用的5种数据持久化方法进行简单 ...
- iOS开发简记(8):数据持久化
数据持久化,也就是把数据保存到磁盘,以后可以再读取出来使用(也可以再次更改或删除).很多场景需要数据持久化,比如为了减轻服务器的访问与存储压力,客户端需要在本地做一些数据持久化的工作. iOS的数据持 ...
- iOS之数据持久化方案
概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...
随机推荐
- springboot 注入xml自定义类
新建入口类可扫描类: @Configuration @ImportResource(locations = {"classpath:spring-bean.xml"}) publi ...
- Tuples as return values
Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is ...
- [C++] upper_bound和lower_bound
upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...
- tomcat到底是干什么用的?用大白话讲一下
通俗点说他是jsp网站的服务器之一,就像asp网站要用到微软的IIS服务器,php网站用apache服务器一样,因为你的jsp动态网站使用脚本语言等写的,需要有服务器来解释你的语言吧,服务器就是这个功 ...
- Tomcat下没有编译后的class文件
输出的路径是否正确: Default output folder: 如果tomcat下还没有classes文件则没有编译好 需要重新引入jar包, clean工程,并重新部署项目. 这样就会在tomc ...
- 【基础篇】Android中获取Drawable的方法
public static Drawable getDrawable(Context context,String filename) { BitmapDrawable drawable=null; ...
- NodeJS学习笔记 进阶 (9)express+cookie-parser:签名机制深入剖析(ok)
个人总结:这篇文章讲解了express框架种cookie的使用,需要引用cookie-parser这个包.读完这篇文章需要10分钟. 摘选自网络 文章导读 cookie-parser是Express的 ...
- Unity Shader (四)顶点程序示例
1.在顶点函数中实现凸起效果 Shader "Custom/Example" { properties { _R(,))= //圆的半径,也是凸起的范围 _OX(,))= //x轴 ...
- ArcGIS api for javascript——明确的创建图层列表
描述 本例展示了如何确切地创建一个地图服务里的图层列表.这个列表由HTML checkboxe组成,可用用于开关图层的可见性. 函数updateLayerVisibility()包含开关图层的逻辑.函 ...
- cocos2d-x 显示触摸操作(显示水波点击效果,用于视频演示)
昨天刚刚參加玩游戏设计大赛, 积累了一些东西. 接下去将会逐个分享出来. 首先是显示触摸操作. 由于要演示我们的作品.使用试玩过程中, 假设没办法显示我们的触摸操作(像录制视频一样, 点击了屏幕某点, ...