ioscore-datasettingsdefault-database

how to have existing data in coredata ENTITY before the app launches


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.


Solution

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