iphone core data 基础操作
来源:互联网 发布:win10软件字体模糊 编辑:程序博客网 时间:2024/05/20 20:02
1. 插入
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [app managedObjectContext];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"entityname" inManagedObjectContext:context];
[newManagedObject setValue:value forKey:@"propertyname"];
NSError *error; if (![context save:&error]) {
// Handle the error…
}
//查询
//删除
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
[context deleteObject:[objecs objectIndex:index];
// Save the context.
NSError *error; if (![context save:&error]) {
// Update to handle the error appropriately.
NSLog(@”Unresolved error %@, %@”, error, [error userInfo]);
exit(-1); // Fail
}
//属性
NSManagedObject *managedObject;
NSString *keypath;
NSString *labelString;
NSString *currentValue = [self.managedObject valueForKeyPath:self.keypath];
NSEntityDescription *ed = [self.managedObject entity];
NSDictionary *properties = [ed propertiesByName];
NSAttributeDescription *ad = [properties objectForKey:self.keypath];
NSString *defaultValue = nil;
if (ad != nil)
defaultValue = [ad defaultValue];
//core data relation 查询 或 修改
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName: @"Person" inManagedObjectContext:thePerson.managedObjectContext];
NSMutableSet *children = [person mutableSetValueForKey:@"children"]; //查询,可修改
[children addObject:child];
[children removeObject:childToBeRemoved];
[[children managedObjectContext] deleteObject:childToBeRemoved]; //真正的删除
NSSet *children = [person valueForKey:@"children"]; //查询,不可修改
for (NSManagedObject *oneChild in children) {
// do something
}
补充:
1 是否将图片存储到Core Data中,以及NSData如何存储的一些规则
First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.
Second, the rule for storing binary data is:
- < 100kb store in the same table as the relevant data
- < 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
- > 1mb store on disk and reference it inside of Core Data
(1。图片尽量保存为文件 2。<100k 和相关数据保存在一张表中 <1M保存在单独的一张表中 >1M保存到磁盘文件)
- iphone core data 基础操作
- iphone core data 基础操作
- iPhone:Core Data 相关文章
- iphone--core data学习笔记
- #iOS# Core Data基础
- Core Data基础
- iphone数据存储之-- Core Data的基础使用
- iPhone Core Data: Your First Steps
- iphone 数据存储--Core-Data的使用
- iphone 数据存储--Core-Data的使用
- Core Data基础概念总结
- Core Data操作模板1
- Core Data操作模板2
- core data CRUD简单操作
- Core Data系列文章(一)Core Data基础
- [ios开发基础之 Core Data[3]]如何使用NSFetchedResultsController 来集成TableView操作
- Core Data浅谈系列之一 : 基础结构
- Core Data浅谈系列之一 : 基础结构
- DATAWINDOW技巧
- 二 Linux的基本操作
- OANET(三):建立arm-linux交叉编译环境
- Zoj 3203 Light Bulb
- 程序员接项目
- iphone core data 基础操作
- 数据库常用操作3
- 【VC报错】LeaveCriticalSection中断异常
- IMP-00041: 警告: 创建的对象带有编译警告解决办法
- Android eclipse中程序调试
- 777
- 51单片机的data,xdata,code
- hadoop操作命令
- poj 3984 迷宫问题(bfs)