RegularExpression
RegularExpression

Reputation: 3541

Leak and What to Do About It

According to Instruments the following has a leak and I don't understand why. Can anyone assist with this? Any help appreciated. Thank you.

- (User *) findUser:(NSString *)userName
{
    NSFetchRequest *userFetch = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    [userFetch setPredicate:[NSPredicate predicateWithFormat:@"userName = %@", userName]];
    NSError *error = nil;
    NSArray *fetchedUser = [[self context] executeFetchRequest:userFetch error:&error];

    if (error)
    {
        NSLog(@"findUser Error: %@", error);
        return nil;
    }
    else if ([fetchedUser count] < 1)
    {
        return nil;
    }
    else
    {
        if ([fetchedUser count] != 1)   // should always be zero or 1.
        {
            return nil;
        }

        User *user = (User *)[fetchedUser objectAtIndex:0];
        return user;
    }
}

The call happens here:

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    . . .

   NSString *uName = ((User *)[currentUserList objectAtIndex:indexPath.row]).userName;
   uManager.userObj = [svoDataHelper findUser:uName];
   [detail didSelectUserWithName:[uManager userName]];
}

Upvotes: 0

Views: 31

Answers (1)

Mundi
Mundi

Reputation: 80265

Your code is very verbose. Try this just after fetching: (I'm using result instead of fetchedUser).

return result.count == 1 ? result.firstObject : nil;

The leak is likely happening elsewhere, most likely in your opaque data helper and data manager classes. One strategy is to try to do the fetch in didSelectRowAtIndexPath instead and see if you still get the leak.

Upvotes: 1

Related Questions