views:

59

answers:

2

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!!

A: 

It looks like you're removing your player objects from your various internal arrays, but not deleting them from the NSManagedObjectContext itself.

Shaggy Frog
I'm a tad confused as to what you mean by this. I don't want to delete the objects in the persistant store itself, such as with "[managedObjectContext deleteObject:objectToDelete];" The objects should never be deleted in that way, but I do want them cleared from the arrays, as those arrays hold which objects are being displayed to the User. However, do you mean you can remove them from the context, so they need to be reloaded again? Or something similar?
Wayfarer
Sorry, I guess I'm confused as I didn't understand your question. I still don't after re-reading it. Are you saving state in the persistent store related to if the entities are currently on-screen? If so, why?
Shaggy Frog
A: 

After a lot more debugging (Quite a bit) I found the issue. The code worked fine, until you "Removed" the first player, or the player in slot 0. I looked at when the players are filtered:

while (j < [objects count]) {
        if ([[[objects objectAtIndex:j] valueForKey:@"currentMultiPlayer"] boolValue] == NO) {
            [objects removeObjectAtIndex:j];
            j--;
        }
        else {
            j++;
        }
    }

And realized that if the first player is not playing, then j becomes -1, and then it breaks out of the loop and fails to validate the rest of the players, causing the weird bug to have them all back in the game. It had nothing to do with memory management at all, but I did fix some leaks in my effort to find the bug. Thank you!

Wayfarer