0 votes
165 views
asked in Objective C by (3.1k points)
Hi all

    How to save,update,delete value from CoreData in iOS

2 Answers

0 votes
answered by (4k points)

- (void) saveValue{

     NSError *error;

    

    [settingsInfo setValue:[NSNumber numberWithInt:10] forKey:@"volume"];

    

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

        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);

    }

}

 

- (void) updateValue{

     NSError *error;

    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Settings"

                                              inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

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

    for (Settings *info in SettingsObjects) {

        NSLog(@"Name: %@", info.volume);

        info.volume = [NSNumber numberWithInt:40];       

        

    }

}

 

- (void) deleteValue{

    NSError *error;

    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Settings"

                                              inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

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

    for (Settings *info in SettingsObjects) {

        [context deleteObject:info];

        

    }

}

 

 

0 votes
answered by (4k points)

- (void) readValue{

    NSError *error;

    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Settings"

                                              inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

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

    for (Settings *info in SettingsObjects) {

        NSLog(@"Name: %@", info.volume);        

    }

}

 

Categories

...