1

In my iOS app - I have something called settings where I set the priority for X , Y ,Z ; eg: X = 30, Y = 60 , Z = 80.

which have some default value (X=50,Y=50 and Z =50) for the first time and untill user changes it to desired value.

Basically user can change the priority N number of times and whenever he changes I just need to update the default values with changed value.

What I need is to save the changed value so, that when he logs in next time he should see his changed values and not default values.

2 Answers2

2

NSUserDefaults would be a better place to do this. You can register defaults you want to be available at initial startup using registerDefaults:. If the user does nothing, you will have the values available to you that you registered. If the user changes a value with one of the keys you register, then those new values will be retrieved rather than the default ones.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • NSUserDefaults to save the information entered even if you “really” close the app. This will allow the data to persist through multiple sessions. But, I want my CoreData Entity PRIORITY(Priority_name, Percentage) to have 3 rows with values below as default. X , 30 Y , 60 Z , 80 – Sunil Panda May 24 '15 at 16:46
  • @SunilPanda Do you want that entity to have those values only until the user changes it? So if the use changes those values, closes the app, and then comes back, do you want to see those changed values or your default ones? – rdelmar May 25 '15 at 05:03
  • I want to have pre populated tuples with default values in it .. actually, I figured out last night , will share it here. – Sunil Panda May 25 '15 at 06:30
0

CoreData doesn't provide any facility to create default tuples when the database is setup. SO, for the user to see some default values which he can change the state in future and see the desired changed value whenever he logs in - he needs to create the database tuples with default values in it. This can be done by using a NSPredicate to check if you have the entry in the tuple and then creating it when result count is 0. Below I am checking if X is present in my DB, else I create all default tuples , because there is a check , it will be called only once in the whole application life cycle.

-(void) viewDidAppear:(BOOL)animated
{
    //fetch priority levels
    [self setDefaultPriorityLevel];

}

-(void)setDefaultPriorityLevel{
    //Do the database setup to default values if the user is logged in first time
    AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext * context = [appDelegate managedObjectContext];
    NSEntityDescription * entityDesc = [NSEntityDescription entityForName:@"ENTITY_NAME" inManagedObjectContext:context];

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

    //just check with anyone no need to check with each relation name
    NSPredicate * predDefault = [NSPredicate predicateWithFormat:@"(relation_name = %@)",@"X"];
    [requestDefault setPredicate:predDefault];

    NSError *error;
    NSArray *objectsForDefault = [context executeFetchRequest:requestDefault error:&error];

    if ([objectsForDefault count] == 0) {
        //no matches create and add the three objects - X, Y, Z (default tuples)
        NSLog(@"No Matches");
        //insert X level
        NSManagedObject *defaultFamilyLevel;
        defaultFamilyLevel = [NSEntityDescription insertNewObjectForEntityForName:@"ENTITY_NAME" inManagedObjectContext:context];
        [defaultFamilyLevel setValue:@"X" forKey:@"relation_name"];
        [defaultFamilyLevel setValue:@"30" forKey:@"relation_priority"];

        //insert Y level
        NSManagedObject *defaultLoveLevel;
        defaultLoveLevel = [NSEntityDescription insertNewObjectForEntityForName:@"ENTITY_NAME" inManagedObjectContext:context];
        [defaultLoveLevel setValue:@"Y" forKey:@"relation_name"];
        [defaultLoveLevel setValue:@"80" forKey:@"relation_priority"];

        //insert Z level
        NSManagedObject *defaultFriendLevel;
        defaultFriendLevel = [NSEntityDescription insertNewObjectForEntityForName:@"ENTITY_NAME" inManagedObjectContext:context];
        [defaultFriendLevel setValue:@"Z" forKey:@"relation_name"];
        [defaultFriendLevel setValue:@"50" forKey:@"relation_priority"];

        [context save:&error];
        NSLog(@"database is created ");

    }
}