3 回答
TA贡献1871条经验 获得超13个赞
更新就像创建一个新的一样简单。
要更新特定的对象,您需要设置一个NSFetchRequest。此类等效于SQL语言中的SELECT声明。
这里有个简单的例子:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Favorits" inManagedObjectContext:moc]];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
// error handling code
该数组results包含sqlite文件中包含的所有托管对象。如果要获取特定对象(或多个对象),则需要对该谓词使用谓词。例如:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@", @"Some Title"];
[request setPredicate:predicate];
在这种情况下,results包含标题等于的对象Some Title。设置谓词等于将WHERE子句放在SQL语句中。
有关更多信息,建议您阅读Core Data编程指南和NSFecthRequest类参考。
核心数据编程指南
NSFecthRequest类参考
希望能帮助到你。
编辑(可用于更新的代码段)
// maybe some check before, to be sure results is not empty
Favorits* favoritsGrabbed = [results objectAtIndex:0];
favoritsGrabbed.title = @"My Title";
// save here the context
或者如果您不使用NSManagedObject子类。
// maybe some check before, to be sure results is not empty
NSManagedObject* favoritsGrabbed = [results objectAtIndex:0];
[favoritsGrabbed setValue:@"My title" forKey:@"title"];
// save here the context
在这两种情况下,如果您save根据上下文进行操作,数据都会被更新。
TA贡献1873条经验 获得超9个赞
希望对您有帮助。因为它对我有用。
NSMutableArray *results = [[NSMutableArray alloc]init];
int flag=0;
NSPredicate *pred;
if (self.txtCourseNo.text.length > 0) {
pred = [NSPredicate predicateWithFormat:@"courseno CONTAINS[cd] %@", self.txtCourseNo.text];
flag=1;
} else {
flag=0;
NSLog(@"Enter Corect Course number");
}
if (flag == 1) {
NSLog(@"predicate: %@",pred);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Course"];
[fetchRequest setPredicate:pred];
results = [[self.context executeFetchRequest:fetchRequest error:nil] mutableCopy];
if (results.count > 0) {
NSManagedObject* favoritsGrabbed = [results objectAtIndex:0];
[favoritsGrabbed setValue:self.txtCourseName.text forKey:@"coursename"];
[self.context save:nil];
[self showData];
} else {
NSLog(@"Enter Corect Course number");
}
}
TA贡献1875条经验 获得超5个赞
如果您是一个敏捷的程序员,这可以为您提供帮助:
如果要删除NSManagedObject
就我而言,ID是实体STUDENT的唯一属性
/** for deleting items */
func Delete(identifier: String) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "STUDENT")
let predicate = NSPredicate(format: "ID = '\(identifier)'")
fetchRequest.predicate = predicate
do
{
let object = try context.fetch(fetchRequest)
if object.count == 1
{
let objectDelete = object.first as! NSManagedObject
context.delete(objectDelete)
}
}
catch
{
print(error)
}
}
如果要更新NSManagedObject:
/** for updating items */
func Update(identifier: String,name:String) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "STUDENT")
let predicate = NSPredicate(format: "ID = '\(identifier)'")
fetchRequest.predicate = predicate
do
{
let object = try context.fetch(fetchRequest)
if object.count == 1
{
let objectUpdate = object.first as! NSManagedObject
objectUpdate.setValue(name, forKey: "name")
do{
try context.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}
}
- 3 回答
- 0 关注
- 561 浏览
添加回答
举报