iOS coredata的使用及版本升级
来源:互联网 发布:od软件 编辑:程序博客网 时间:2024/05/19 16:50
//
//
// AppDelegate.m
// CoreData
//
// Created by 王聪on 14/8/19.
// Copyright (c) 2014 年 Congwang. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
// Override point for customization after application launch.
//模型版本升级步骤:
//1.新建模型版本 (选中可视化建模文件, 点击editor, 选择addModelVersion)
//2.把当前新建的模型版本设置成选中状态 (先选中总的模型版本, 然后在属性检查器中的第一个标签有个current,选中新建的即可)
//3.创建映射文件(cmd + n , 在coreData选项卡中, 选择mappingModel), 记住source是老的模型版本, target是新的模型版本
//4.在数据连接器中的懒加载方法中, option选项由nil变成一个字典, key为NSMigrate, value为@{YES}, 该选项的意思是, 模型版本自动升级 (数据库升级)
//
//
//
//
//
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOp
}
- (void)applicationWillResignAct
}
- (void)applicationDidEnterBackg
}
- (void)applicationWillEnterFore
}
- (void)applicationDidBecomeActi
}
- (void)applicationWillTerminate
}
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinat
- (NSURL *)applicationDocumentsDire
}
//
- (NSManagedObjectModel *)managedObjectModel {
}
- (NSPersistentStoreCoordin
}
//数据管理器的懒加载 (重写getter方法)
- (NSManagedObjectContext *)managedObjectContext {
}
#pragma mark - Core Data Saving support
- (void)saveContext {
}
// ViewController.m
// CoreData
//
// Created by 王聪 on 14/8/19.
// Copyright (c) 2014 年 Congwang. All rights reserved.
//
#import "ViewController.h"
#import "Clothes.h"
#import "AppDelegate.h"
@interface ViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) AppDelegate *myAppDelegate;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[ super viewDidLoad];
self.myAppDelegate = [UIApplicationsharedApplication].delegate;
//初始化数组
self.dataSource = [NSMutableArray array]; //进行数据查询 -- coreData查询
//1.创建数据查询对象
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Clothes"];
//2.创建排序描述对象(可选择, 想进行排序的时候在写)
NSSortDescriptor *sortDescription = [[NSSortDescriptoralloc] initWithKey:@"price" ascending:YES];
//2.1给request的排序属性赋值
request. sortDescriptors = @[sortDescription];
// //3. 创建检索条件(可选择)
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and price > %@", @"Nike", [NSNumber numberWithFloat:1.0]];
// request.predicate = predicate;
NSError *error = nil;
NSArray *resultArray = [self.myAppDelegate.managedObjectContextexecuteFetchRequest:request error:&error];
//
[ self.dataSource addObjectsFromArray:resultArray];
}
#pragma mark - tableView的代理和dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :@"cell"];
Clothes *cloth = self.dataSource[indexPath.row];
cell. textLabel.text = [NSString stringWithFormat:@"%@ -- %@", cloth.name, cloth.price];
return cell;
}
//点击cell触发的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//修改数据
Clothes *cloth = self.dataSource[indexPath.row];
cloth. name = @"Nike";
//将修改进行永久保存
[ self.myAppDelegate saveContext];
//刷新单元格
[ self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic ];
}
//允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete ) {
//1.先删除数据源
Clothes *clothe = self.dataSource[indexPath.row];
//在关于布局dataSource中, 删除数据
[ self.dataSource removeObject:clothe];
//要在真实的文件中删除 (*coreData中的删除)
[ self.myAppDelegate.managedObjectContextdeleteObject:clothe];
//对数据管理器进行改变, 保存到真实的文件当中
[ self.myAppDelegate saveContext];
//2.再删除单元格
[ self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade ];
}
}
//添加数据
- (IBAction)addModel:(id)sender {
// 1.先插数据源
//coreData中持久化操作
//快速插入方法 (推荐用)
Clothes *clothNew = [NSEntityDescriptioninsertNewObjectForEntityForName :@"Clothes"inManagedObjectContext:self.myAppDelegate.managedObjectContext];
//给对象的属性赋值
//实体描述
NSEntityDescription *description = [NSEntityDescriptionentityForName:@"Clothes"inManagedObjectContext:self.myAppDelegate.managedObjectContext];
//创建模型对象
Clothes *clothe = [[Clothes alloc]initWithEntity:description insertIntoManagedObjectContext :self.myAppDelegate.managedObjectContext];
clothe. name = @"Prada";
float price = (arc4random() % 1000 +1) / 1.1;
clothe. price = [NSNumber numberWithFloat:price];
//添加到数据源中 插入数据源
[ self.dataSource addObject:clothe];
// 2.插入单元格
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:self.dataSource.count - 1 inSection:0];
[ self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft ];
//对数据管理器中的数据进行永久保存 (保存在沙盒路径下的真是的文件中)
[ self.myAppDelegate saveContext];
}
- (void)didReceiveMemoryWarning {
[ super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
//
//
//
//
#import "ViewController.h"
#import "Clothes.h"
#import "AppDelegate.h"
@interface ViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) AppDelegate *myAppDelegate;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
//
//
//
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingSt
}
- (IBAction)addModel:(id)sender {
}
- (void)didReceiveMemoryWarning {
}
@end
0 0
- iOS coredata的使用及版本升级
- iOS coredata的使用及版本升级
- Coredata的版本升级
- iOS 使用CoreData Model Version版本升级处理
- CoreData的操作(使用, 升级)
- iOS 版本迭代 App升级安装 CoreData数据库升级
- CoreData(数据库升级 )版本迁移-iOS App升级安装
- CoreData(数据库升级 )版本迁移-iOS App升级安装
- IOS - CoreData的使用
- iOS CoreData的使用
- Coredata版本升级注意事项
- ios coreData 根据应用版本升级进行更新
- IOS APP 升级安装-coredata 的迁移,升级
- ios中的coredata的使用
- IOS CoreData的初始化使用
- ios中的coredata的使用
- ios中的coredata的使用
- ios中的coredata的使用
- iOS AFN进行断点下载
- iOS masonry进行页面适配
- com.mysql.jdbc.MysqlDataTruncation: Data truncation异常
- iOS M3U8视频的下载与播放
- 新建SVN仓库设置
- iOS coredata的使用及版本升级
- 活动指示器ActivityIndicatorView和进度条ProgressView
- iOS block传值
- 建枚举类时出现“Project '**' is not a J2SE 5.0 compliant project.”问题怎么办?
- iOS 百度地图
- 线段树 csu1555 Inversion Sequence
- 第一章 java前奏
- 《Python基础教程 第2版·修订版》第3章 使用字符串(学习笔记)
- Java版线性表的链式存储和实现