如何更新核心数据中的现有对象?

当我插入新对象时,我将使用以下代码:


NSManagedObjectContext *context = [appDelegate managedObjectContext];


Favorits *favorits = [NSEntityDescription insertNewObjectForEntityForName:@"Favorits" inManagedObjectContext:context];


favorits.title = @"Some title";


NSError *error;                    

if (![context save:&error]) {

    NSLog(@"Whoops");

}

如何更新核心数据中的现有对象?


德玛西亚99
浏览 562回答 3
3回答

慕桂英4014372

更新就像创建一个新的一样简单。要更新特定的对象,您需要设置一个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 emptyFavorits* favoritsGrabbed = [results objectAtIndex:0];    favoritsGrabbed.title = @"My Title";// save here the context或者如果您不使用NSManagedObject子类。// maybe some check before, to be sure results is not emptyNSManagedObject* favoritsGrabbed = [results objectAtIndex:0];[favoritsGrabbed setValue:@"My title" forKey:@"title"];// save here the context在这两种情况下,如果您save根据上下文进行操作,数据都会被更新。

眼眸繁星

希望对您有帮助。因为它对我有用。 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");    }}

慕田峪4524236

如果您是一个敏捷的程序员,这可以为您提供帮助:如果要删除NSManagedObject就我而言,ID是实体STUDENT的唯一属性/** for deleting items */func Delete(identifier: String) {&nbsp; &nbsp; let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext&nbsp; &nbsp; let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "STUDENT")&nbsp; &nbsp; let predicate = NSPredicate(format: "ID = '\(identifier)'")&nbsp; &nbsp; fetchRequest.predicate = predicate&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; let object = try context.fetch(fetchRequest)&nbsp; &nbsp; &nbsp; &nbsp; if object.count == 1&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let objectDelete = object.first as! NSManagedObject&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;context.delete(objectDelete)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; print(error)&nbsp; &nbsp; }}&nbsp;如果要更新NSManagedObject:/** for updating items */func Update(identifier: String,name:String) {&nbsp; &nbsp; let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext&nbsp; &nbsp; let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "STUDENT")&nbsp; &nbsp; let predicate = NSPredicate(format: "ID = '\(identifier)'")&nbsp; &nbsp; fetchRequest.predicate = predicate&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; let object = try context.fetch(fetchRequest)&nbsp; &nbsp; &nbsp; &nbsp; if object.count == 1&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let objectUpdate = object.first as! NSManagedObject&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectUpdate.setValue(name, forKey: "name")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try context.save()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(error)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; print(error)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP