Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

objective c - delete and Update Data in Core Data in iOS

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] 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 ..?

I have gone through this link http://www.appcoda.com/core-data-tutorial-update-delete/

Thank in Advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];
    }

and for Update :

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...