Domanda

I'm having an issue with my app - I'm using NSUserDefaults to save a 'profile' for the user that is currently using the app.

It takes some information "name, employee ID, Address and Base School" - pretty standard stuff really. It saves fine and holds the information.

baseSchool is selected with a UIPickerView that has a list of schools - it uses a method called schoolNameList from my MetaMiles class to populate the pickerview with a list of schools.

I use that same method in another view when users try to 'add mileage' to the app in order to populate those UIPickerViews with the same list of schools.

So the process looks like this:

  1. User launches app and sets up their profile (clicks save - everything saves to NSUserDefaults) - you can open and save the profile repeatedly with no trouble.

  2. User clicks "add miles" - add miles page displays with a pickerview of schools and they add their miles.

  3. User clicks "change profile" - app crashes.

The app crashes because at some point along the line - the NSUserDefaults has forgotten the baseSchool that was set in step 1 (when user sets up profile the first time). It appears to only happen if the user clicks "add miles" and the pickerviews from that view are loaded.

So I'm wondering if it has something to do with that method - or if maybe I'm not properly saving my defaults? Perhaps I should be using coredata and saving user profile information to an entity?

Any advice/feedback appreciated.

Here is my profile view:

    //Call method to get array of school names (reusable)
    MetaMiles *model = [MetaMiles schoolNameList];
    _schoolList = (NSArray *)model;

    //****LOAD USER DATA IF AVAILABLE****//
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"profileSetup"]==0){

    } else {
        _fullName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"fullName"];
        _employeeID.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"employeeID"];
        _homeAddress.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"homeAddress"];

        //****//****THIS IS WHERE THE CRASH OCCURS BECAUSE IT DOES NOT REMEMBER DEFAULT AFTER THE ADD MILES VIEW HAS BEEN VIEWED****//****//
        NSString *lastSelectedSchool =[[NSUserDefaults standardUserDefaults] objectForKey:@"baseSchool"];
        //If the add miles view hasn't been loaded this works fine - otherwise it is null
        NSLog(@"Last Selected School from defaults: %@",lastSelectedSchool);  
        NSUInteger currentIndex = [_schoolList indexOfObject:lastSelectedSchool];
        //This doesn't work if it's null!
        NSLog(@"Integer of school: %lu", (unsigned long)currentIndex);
        [schoolPicker selectRow:currentIndex inComponent:0 animated:YES];  
    }
    //****END LOAD USER DATA****//

And here is my MetaMiles class method

+ (NSArray*)schoolNameList {
    NSArray *schoolList;
    MetaMiles *school;
    //Create fetched results controller of data
    NSFetchedResultsController *fetchedSchoolController = [MetaMiles MR_fetchAllSortedBy:@"beg_school" ascending:YES withPredicate:nil groupBy:@"beg_school" delegate:nil];
    [fetchedSchoolController.fetchRequest setReturnsDistinctResults:YES];
    //Turn the controller into an array called - fetchedSchools
    NSArray *fetchedSchools = [fetchedSchoolController fetchedObjects];
    //Create a set of school names - mutable set (only 1 listing per school)
    NSMutableSet *schoolNames = [[NSMutableSet alloc]init];
    //place each school name into the Set
    for (school in fetchedSchools) {
        [schoolNames addObject:school.beg_school];
    }
    //Take the set, turn it into an array (schoolList) and order it by school name
    schoolList = [[schoolNames allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES]]];
    return schoolList;
}
@end
È stato utile?

Soluzione

You placed :

selectedSchool = [NSString stringWithFormat:@"%@",
                          [_schoolList objectAtIndex:row]];

in -didSelectRow: but you save the profile by hitting an button ((IBAction)saveProfileButton:(id)sender).

This is why baseSchool is null .

NSUserDefault crashes with null values.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top