iOS学习爬坑记录12:学习Core data简单的CURD
来源:互联网 发布:java获取浏览器语言 编辑:程序博客网 时间:2024/05/16 11:50
这是参考了前人工作“Core Data浅谈系列之二 : 简单的CURD”:http://blog.csdn.net/jasonblog/article/details/8526148
CURD指creat、update、retrieve和delete。把他的代码在Xcode 6.1下重现了一遍,中间遇到点错误,耽搁了两天,主要是这个项目在建工程的时候不添加core data支持(否则core data相关代码会出现在AppDelegate文件中),而且建立工程后不添加xcdatamodeld文件,都怪我理解不深。
相关代码如下:
ViewController.h文件
#import <UIKit/UIKit.h>#import <CoreData/CoreData.h>@interface ViewController : UIViewController@property (nonatomic, retain) NSManagedObjectModel *managedObjectModel;@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;@property (nonatomic, retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;@end
ViewController.m文件
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self createTeamWithName:@"Heat"city:@"Miami"]; [self createTeamWithName:@"Lakers"city:@"LA"]; [self saveContext]; NSArray *teamArray = [self fetchTeamList]; if (teamArray) { for (NSManagedObject *teamObject in teamArray) { NSString *teamName = [teamObject valueForKey:@"name"]; NSString *teamCity = [teamObject valueForKey:@"city"]; NSLog(@"Team info : %@, %@\n", teamName, teamCity); } }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark -#pragma mark CURD Operations- (BOOL)createTeamWithName:(NSString *)teamName city:(NSString *)teamCity{ if (!teamName || !teamCity) { return NO; } NSManagedObject *teamObject = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext]; [teamObject setValue:teamName forKey:@"name"]; [teamObject setValue:teamCity forKey:@"city"]; return YES;}- (NSArray *)fetchTeamList{ NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:[NSEntityDescription entityForName:@"Team" inManagedObjectContext:self.managedObjectContext]]; NSError *error = NULL; NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; if (error) { NSLog(@"Error : %@\n", [error localizedDescription]); } fetchRequest = nil; return array;}#pragma mark -#pragma mark Save Context- (void)saveContext{ NSError *error = NULL; NSManagedObjectContext *moc = self.managedObjectContext; if (moc && [moc hasChanges] && ![moc save:&error]) { NSLog(@"Error %@, %@", error, [error localizedDescription]); abort(); }}#pragma mark -#pragma mark Core Data Stack- (NSManagedObjectModel *)managedObjectModel{ if (nil != _managedObjectModel) { return _managedObjectModel; } _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; return _managedObjectModel;}- (NSManagedObjectContext *)managedObjectContext{ if (nil != _managedObjectContext) { return _managedObjectContext; } _managedObjectContext = [[NSManagedObjectContext alloc] init]; if (self.persistentStoreCoordinator) { [_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator]; } return _managedObjectContext;}- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{ if (nil != _persistentStoreCoordinator) { return _persistentStoreCoordinator; } NSString *storeType = NSSQLiteStoreType; NSString *storeName = @"cdNBA.sqlite"; NSError *error = NULL; NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:storeName]]; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; if (![_persistentStoreCoordinator addPersistentStoreWithType:storeType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Error : %@\n", [error localizedDescription]); NSAssert1(YES, @"Failed to create store %@ with NSSQLiteStoreType", [storeURL path]); } return _persistentStoreCoordinator;}#pragma mark -#pragma mark Application's Documents Directory- (NSString *)applicationDocumentsDirectory{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return basePath;}@end
中间过程爬坑的时候,犯了两个错误,一次是添加了core data支持又添加了xcdatamodeld文件(这里写的core data相关代码和系统自动生成的core data代码还是有差别的),还有一次是添加了xcdatamodeld文件,只要你添加了一次,后面怎么调试都会报错“"NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'"”,最后我参考了http://www.cnblogs.com/mybkn/archive/2012/09/03/2669412.html,把我手机上(真机调试)的调试应用删除,再编译运行就好了,记住,这里不能添加xcdatamodeld文件。
————————————————————————————————————————————————
这里更正一下,创建工程时添加core data支持也是可以的,而且可以减少代码量,只是需要稍作修改。而且,以后调试代码要注意,如果有本地文件,在真机上运行出错的时候会不妨把app删掉重新运行Xcode。在有core data支持后,ViewController.m代码全部如下:
#import "ViewController.h"#import "AppDelegate.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; [self createTeamWithName:@"Heat"city:@"Miami"]; [self createTeamWithName:@"Lakers"city:@"LA"]; [appDelegate saveContext]; NSArray *teamArray = [self fetchTeamList]; if (teamArray) { for (NSManagedObject *teamObject in teamArray) { NSString *teamName = [teamObject valueForKey:@"name"]; NSString *teamCity = [teamObject valueForKey:@"city"]; NSLog(@"Team info : %@, %@\n", teamName, teamCity); } }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark -#pragma mark CURD Operations- (BOOL)createTeamWithName:(NSString *)teamName city:(NSString *)teamCity{ //managedObjectContext 定义在AppDelegate.h文件 AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; NSManagedObjectContext *context = [appDelegate managedObjectContext]; if (!teamName || !teamCity) { return NO; } NSManagedObject *teamObject = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:context]; [teamObject setValue:teamName forKey:@"name"]; [teamObject setValue:teamCity forKey:@"city"]; return YES;}- (NSArray *)fetchTeamList{ //managedObjectContext 定义在AppDelegate.h文件 AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:[NSEntityDescription entityForName:@"Team" inManagedObjectContext:context]]; NSError *error = NULL; NSArray *array = [context executeFetchRequest:fetchRequest error:&error]; if (error) { NSLog(@"Error : %@\n", [error localizedDescription]); } //[fetchRequest release], fetchRequest = nil; return array;}@end
- iOS学习爬坑记录12:学习Core data简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- iOS学习笔记--Core Data的使用
- 【iOS基础】学习 Core Data 过程记录【待更】
- core data的学习
- ThinkPHP学习简单的CURD操作
- iOS学习笔记——Core Data
- iOS学习笔记——Core Data
- IOS学习笔记——Core Data
- IOS学习笔记——Core Data
- iOS学习笔记——Core Data
- iOS 开发 - iOS Core Animation学习记录
- iOS学习笔记14(1)—Core Data简单介绍和实现
- 编写uefi的lib时的一个错误
- Xshell初探
- ubuntu如何修改计算机名
- yii2 打开GII 的配置
- db2
- iOS学习爬坑记录12:学习Core data简单的CURD
- Swift语言IOS8开发战记9.transfer data without stroyboard
- cocopods 升级和选择安装版本
- iOS Xcode6 AutoLayout跟Size Classes(5)size classes 屏幕旋转的应用
- Longest Consecutive Sequence
- 模块化编程(学习)
- ps筛选特定进程再kill掉的组合命令
- ubuntu14.04交叉编译vlc2.1.5源码,编译出在win32下运行的程序
- IIS默认拒绝PUT和DELETE请求