在 iOS 中删除和更新 Core Data 中的数据 [英] delete and Update Data in Core Data in iOS

查看:16
本文介绍了在 iOS 中删除和更新 Core Data 中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 中 Core Data 的新手,我实现了 4 个文本字段,即姓名、年龄、公司、地址.现在,当用户输入数据时,我将数据保存在 Core Data 中.

Hi i am a newbie in Core Data in iOS, I have implemented a 4 textfields namely name,age,company,address. Now when the user enter the data i am saving the data in Core Data.

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

NSManagedObject *newContact;
newContact = [NSEntityDescription
              insertNewObjectForEntityForName:@"Device"
              inManagedObjectContext:context];
[newContact setValue: _name.text forKey:@"name"];
[newContact setValue: _address.text forKey:@"address"];
[newContact setValue: _age.text forKey:@"age"];
[newContact setValue:_company.text forKey:@"company

同样,我能够在这些文本字段中获取和显示数据.

Similarly i am able to fetch and display the Data in these textfields.

AppDelegate *appDelegate =[[UIApplication sharedApplication] 委托];

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Device"
            inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];

NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(name = %@)",
 _name.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;

NSError *error;
NSArray *objects = [context executeFetchRequest:request
                                          error:&error];matches = objects[0];

    _address.text = [matches valueForKey:@"address"];
    _age.text = [matches valueForKey:@"age"];
    _company.text = [matches valueForKey:@"company"];

现在,虽然我想删除和更新特定用户的数据,但我无法获取数据.

Now while i want to delete and Update the data of the particular User i am not able to fetch the data.

如何删除数据并更新..?

How can i delete the data and Update ..?

我已经浏览了这个链接http://www.appcoda.com/core-data-tutorial-update-删除/

提前致谢

推荐答案

你可以像这样删除数据:

you can delete data like :

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"entityname" inManagedObjectContext:context];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID like %@",userID];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];

    NSError *error;
    NSArray *items = [context executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject *managedObject in items)
    {
        [context deleteObject:managedObject];
    }

和更新:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"entityname" inManagedObjectContext:context];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID like %@",userID];
    [fetchRequest setPredicate:predicate];
    [fetchRequest setFetchLimit:1];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSArray *arrResult = [context executeFetchRequest:fetchRequest error:&error];
YourEntityname *entity = arrResult[0];
entity.userID = @"2"
[appdelegate saveContext];

这篇关于在 iOS 中删除和更新 Core Data 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆