Losing NSManaged Objects in my Application

Posted by Wayfarer on Stack Overflow See other posts from Stack Overflow or by Wayfarer
Published on 2010-06-07T20:29:12Z Indexed on 2010/06/07 20:32 UTC
Read the original article Hit count: 283

I've been doing quite a bit of work on a fun little iPhone app. At one point, I get a bunch of player objects from my Persistant store, and then display them on the screen. I also have the options of adding new player objects (their just custom UIButtons) and removing selected players.

However, I believe I'm running into some memory management issues, in that somehow the app is not saving which "players" are being displayed.

Example: I have 4 players shown, I select them all and then delete them all. They all disappear. But if I exit and then reopen the application, they all are there again. As though they had never left. So somewhere in my code, they are not "really" getting removed.

MagicApp201AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    context = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *desc = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:context];
    [request setEntity:desc];
    NSError *error;
    NSMutableArray *objects = [[[context executeFetchRequest:request error:&error] mutableCopy] autorelease];
    if (objects == nil) {
        NSLog(@"Shit man, there was an error taking out the single player object when the view did load. ", error);
    }
    int j = 0;
    while (j < [objects count]) {
        if ([[[objects objectAtIndex:j] valueForKey:@"currentMultiPlayer"] boolValue] == NO) {
            [objects removeObjectAtIndex:j];
            j--;
        }
        else {
            j++;
        }
    }
    [self setPlayers:objects]; //This is a must, it NEEDS to work Objects are all the players playing

So in this snippit (in the viewdidLoad method), I grab the players out of the persistant store, and then remove the objects I don't want (those whose boolValue is NO), and the rest are kept. This works, I'm pretty sure. I think the issue is where I remove the players. Here is that code:

                                     NSLog(@"Remove players");
                    /**
                     For each selected player:
                     Unselect them (remove them from SelectedPlayers)
                     Remove the button from the view
                     Remove the button object from the array
                     Remove the player from Players
                     */
                    NSLog(@"Debugging Removal: %d", [selectedPlayers count]);
                    for (int i=0; i < [selectedPlayers count]; i++) {
                        NSManagedObject *rPlayer = [selectedPlayers objectAtIndex:i];

                        [rPlayer setValue:[NSNumber numberWithBool:NO] forKey:@"currentMultiPlayer"];


                        int index = [players indexOfObjectIdenticalTo:rPlayer]; //this is the index we need

                        for (int j = (index + 1); j < [players count]; j++) {
                            UIButton *tempButton = [playerButtons objectAtIndex:j];
                            tempButton.tag--;
                        }

                        NSError *error;
                        if ([context hasChanges] && ![context save:&error]) {

                            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                            abort();
                        } 


                        UIButton *aButton = [playerButtons objectAtIndex:index];

                        [players removeObjectAtIndex:index];

                        [aButton removeFromSuperview];

                        [playerButtons removeObjectAtIndex:index];

                    }
                    [selectedPlayers removeAllObjects];

                    NSError *error;
                    if ([context hasChanges] && ![context save:&error]) {
                        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                        abort();
                    } 


                    NSLog(@"About to refresh YES");
                    [self refreshAllPlayers:YES];

The big part in the second code snippet is I set them to NO for currentMultiPlayer. NO NO NO NO NO, they should NOT come back when the view does load, NEVER ever ever. Not until I say so. No other relevant part of the code sets that to YES. Which makes me think... perhaps they aren't being saved. Perhaps that doesn't save, perhaps those objects aren't being managed anymore, and so they don't get saved in. Is there a lifetime (metaphorically) of NSManaged object? The Players array is the same I set in the "viewDidLoad" method, and SelectedPlayers holds players that are selected, references to NSManagedObjects. Does it have something to do with Removing them from the array? I'm so confused, some insight would be greatly appreciated!!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c