【问题标题】:NSArray with nested objects "losing" objects in memory具有嵌套对象的 NSArray 在内存中“丢失”对象
【发布时间】:2014-06-14 00:10:17
【问题描述】:

所以在一个块中,我正在获取并解析一个看起来像这样的 JSON 对象(任何时候你看到“<...>”都假定数据格式正确)(这也只是有问题的对象之一):

{
    asset =         {
        <...>
    };
    <...>
    sections =         (
                    {
            <...>
            fields =                 (
            );
            items =                 (
            );
        },
                    {
            <...>
            fields =                 (
            );
            items =                 (
            );

        },
                    {
            <...>
            fields =                 (
            );
            items =                 (
                                    {
                    <...>
                    properties =                         {
                        title = "We welcome guests...";
                    };
                },
                                    {
                    <...>
                    properties =                         {
                        title = "More text";
                    };
                },
                                    {
                    <...>
                    properties =                         {
                        title = "Opportunity Insert...";
                    };
                }
            );
        },
                    {
            <...>
            fields =                 (
                                    {
                    <...>
                    values =                         (
                        Private,
                        Public
                    );
                },
                                    {
                    <...>
                    values =                         (
                    );
                },
                                    {
                    <...>
                    values =                         (
                    );
                }
            );
            items =                 (
            );
        },
                    {
            <...>
            fields =                 (
                                    {
                    <...>
                    values =                         (
                    );
                },
                                    {
                    <...>
                    values =                         (
                    );
                }
            );
            items =                 (
            );
        }
    );
}

获取此代码的代码(使用 AFNetworking):

NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:AppsURL([[NSUserDefaults standardUserDefaults] objectForKey:kAuthToken])]];

AFHTTPRequestOperation* op = [[AFHTTPRequestOperation alloc] initWithRequest:req];
[op setResponseSerializer:[[AFJSONResponseSerializer alloc] init]];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (responseObject != nil&&[responseObject isKindOfClass:[NSDictionary class]]) {
        NSDictionary* dict = (NSDictionary*)responseObject;
        if (dict[@"apps"]&&[dict[@"apps"] isKindOfClass:[NSArray class]]) {
            apps = dict[@"apps"];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            return;
        }

    }
        NSLog(@"Error Loading apps:%@",responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error:%@",error);
    NSLog(@"Response:%@",operation.response.description);

}];

[op start];

此时,所有内容都已加载到内存中并正常运行。我将其设置为类级别的 NSArray,然后使用该 NSArray 填充表格。当我在用户从表中选择一项后尝试检索数据时,问题就出现了。然后,我使用索引并从 NSArray 中拉出该对象,以找到看起来像这样的对象(请注意,所有项目和字段数组现在都是空的):

{
asset =     {
    <...>
};
<...>
sections =     (
            {
        <...>
        fields =             (
        );
        items =             (
        );
    },
            {
        <...>
        fields =             (
        );
        items =             (
        );
    },
            {
        <...>
        fields =             (
        );
        items =             (
        );
    },
            {
        <...>
        fields =             (
        );
        items =             (
        );
    }
);
}

选择代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  if (indexPath.row < apps.count) {
    //the output of this is what I copy and pasted
    NSLog(@"app data to be cleaned up:%@ \r app data sent:%@",apps[indexPath.row],((NSDictionary*)apps[indexPath.row]).mutableCopy);

    //I originally thought the problem was here, but the output above proved me wrong
    NSMutableDictionary *app = [Functions recurseDictionaryForNull:((NSDictionary*)apps[indexPath.row]).mutableCopy];

    [[NSUserDefaults standardUserDefaults] setObject:app
                                              forKey:@"app"];

    MainMenuViewController* mainMenu = (MainMenuViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"mainMenu"];

    [self.navigationController setViewControllers:@[mainMenu] animated:YES];// reset nav stack
  }
}

我有点不知所措...有什么想法可以做到这一点以及如何解决它??

【问题讨论】:

  • 这里没有足够的信息可供任何人帮助您。您应该提供创建和访问此数组的相关代码。
  • 添加代码,请不要在有人有机会回复之前投反对票...有点粗鲁。
  • 不幸的是,其他人对你投了反对票。
  • 啊,抱歉打来电话。
  • 如果类级apps 包含fieldsitems 的非空嵌套数组,此时表格已填满,但稍后在行选择时为空,那么我建议您调试介于两者之间的代码 - 您未显示的代码是否填充了操纵apps 的表格或其中任何行的任何副本?还要提醒一下,NSDictionary 上的 mutableCopy 不是深层副本 - 您嵌套的 fieldsitems 不是副本。

标签: ios objective-c nsarray nsdictionary


【解决方案1】:

这是来自服务器的数据。我被告知 2 个“应用程序”除了一个项目外是相同的,结果发现它们也缺少所有字段和项目。感谢 Dima 和 Mike 抽出宝贵时间!

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2021-09-16
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多