http://blog.csdn.net/rhljiayou/article/details/18037729
- //删除
- -(void)deleteData
- {
- NSManagedObjectContext *context = [self managedObjectContext];
- NSEntityDescription *entity = [NSEntityDescription entityForName:TableName inManagedObjectContext:context];
- NSFetchRequest *request = [[NSFetchRequest alloc] init];
- [request setIncludesPropertyValues:NO];
- [request setEntity:entity];
- NSError *error = nil;
- NSArray *datas = [context executeFetchRequest:request error:&error];
- if (!error && datas && [datas count])
- {
- for (NSManagedObject *obj in datas)
- {
- [context deleteObject:obj];
- }
- if (![context save:&error])
- {
- NSLog(@"error:%@",error);
- }
- }
- }
- //更新
- - (void)updateData:(NSString*)newsId withIsLook:(NSString*)islook
- {
- NSManagedObjectContext *context = [self managedObjectContext];
- NSPredicate *predicate = [NSPredicate
- predicateWithFormat:@"newsid like[cd] %@",newsId];
- //首先你需要建立一个request
- NSFetchRequest * request = [[NSFetchRequest alloc] init];
- [request setEntity:[NSEntityDescription entityForName:TableName inManagedObjectContext:context]];
- [request setPredicate:predicate];//这里相当于sqlite中的查询条件,具体格式参考苹果文档
- //https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html
- NSError *error = nil;
- NSArray *result = [context executeFetchRequest:request error:&error];//这里获取到的是一个数组,你需要取出你要更新的那个obj
- for (News *info in result) {
- info.islook = islook;
- }
- //保存
- if ([context save:&error]) {
- //更新成功
- NSLog(@"更新成功");
- }
- }