【问题标题】:Leak and What to Do About It泄漏以及如何处理
【发布时间】:2015-01-24 00:15:48
【问题描述】:

根据 Instruments,以下内容有泄漏,我不明白为什么。有人可以帮忙吗?任何帮助表示赞赏。谢谢。

- (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;
    }
}

调用发生在这里:

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

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

【问题讨论】:

    标签: ios core-data memory-leaks


    【解决方案1】:

    您的代码非常冗长。在获取之后试试这个:(我使用result而不是fetchedUser)。

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

    泄漏可能发生在其他地方,最有可能发生在您的不透明数据助手和数据管理器类中。一种策略是尝试在didSelectRowAtIndexPath 中进行提取,看看是否仍然存在泄漏。

    【讨论】:

    • 是的,这段代码非常冗长——它实际上是作为一个存根,因为我在编写它时计划进行一些错误处理。无论如何,感谢您提出移动获取尝试的想法。没想到;我去看看。
    猜你喜欢
    • 2011-12-12
    • 2011-05-30
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 2012-12-27
    • 2015-06-15
    相关资源
    最近更新 更多