views:

23

answers:

1

I'm writing an application that uses a UIPickerView and is storing each change to the pickers in core data (the name of the app is Wizard Blood in the iTunes store). I've had some success so far in that my -didSelectRow method successfully adds new objects to core data via the following code:

// Get context, entity, managedobject from the fetchedResultsController

    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];

    //Iterates through each component and sets the value to the appropriate key
    for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
        NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
        [newManagedObject setValue:[NSNumber numberWithInteger:[LifeCounter selectedRowInComponent:j]] forKey:keyName];
    }

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Error saving entity: %@", [error localizedDescription]);
    }   

However, in my -viewDidLoad I can't seem to get core data to load values from the latest object. This is what I have so far:

lifeCounterArray = [[NSMutableArray alloc] init];

for ( int i = 200; i >= -200; i-- )
{
    NSString *myString = [NSString stringWithFormat:@"%i", i];
    [lifeCounterArray addObject:myString];
}

//Hopefully this retrieves the latest values from the core data store
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

//Iterates through each component, setting it to the stored value in core data
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
    NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
    NSInteger row = [[managedObject valueForKey:keyName] integerValue] +180;
    [LifeCounter selectRow:row inComponent:j animated:NO];
}

Now, I'm pretty sure my fetchedResultsController method is working since I can see it creating objects (I can see them in the sqlite database) but maybe I shouldn't even be using a fetchedResultsController since I really only care about the latest object. I'd be happy to post my fetchedResultsController method if needed, thanks for the help in advance.

A: 

If you've changed the data since you last queried might you have to delete the fetchedResultsController's cache?

[NSFetchedResultsController deleteCacheWithName:@"(cachename)"];

The cache name is set in initWithFetchRequest.

if that doesn't work, try setting the fetchedResultsController to nil after the db changes which will force it to redo the request.

mclin
Yeah, neither of these worked. Instead of even trying to delete the cache, I just set it to nil. I'm also 95% sure that the cache is updated automatically anyway and the only reason you would delete it is to free memory. I also tried setting the fetchedResultsController to nil just before the fetch and it didn't work either. Thanks for trying.
Robot-Scott