亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何更新核心數據中的現有對象?

如何更新核心數據中的現有對象?

德瑪西亞99 2019-10-25 15:04:17
當我插入新對象時,我將使用以下代碼:NSManagedObjectContext *context = [appDelegate managedObjectContext];Favorits *favorits = [NSEntityDescription insertNewObjectForEntityForName:@"Favorits" inManagedObjectContext:context];favorits.title = @"Some title";NSError *error;                    if (![context save:&error]) {    NSLog(@"Whoops");}如何更新核心數據中的現有對象?
查看完整描述

3 回答

?
慕桂英4014372

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根據上下文進行操作,數據都會被更新。


查看完整回答
反對 回復 2019-10-25
?
眼眸繁星

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");

    }




}


查看完整回答
反對 回復 2019-10-25
?
慕田峪4524236

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)

    }

}


查看完整回答
反對 回復 2019-10-25
  • 3 回答
  • 0 關注
  • 596 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號