ios - 由于 'GET' 请求多个对象的内存问题导致应用程序崩溃

标签 ios objective-c xcode

我有一个方法可以检索大量数据并将其存储到数组中,然后将其传递给完成处理程序:

[edcs getClinicalPatientDataWithDictionaryForPatientID:participant.participantNum withStartDate:pastDate withEndDate:currentDate completionBlock:^(NSDictionary *jsonDict) {
    if ([jsonDict[@"status"] isEqualToString:@"200"]) {
        if (jsonDict[@"body"]) {
            NSMutableArray *body = [NSMutableArray  arrayWithObject:jsonDict[@"body"]];
            if (body) {
                // Query all measurements
                for (NSArray *jsonArray in body) {
                    for (NSDictionary *measurementDict in jsonArray) {
                        // Separate Hydration measurements
                        if (![body.description containsString:@"<null>"]) {
                            if (measurementDict) {
                                if ([[measurementDict objectForKey:@"MeasureUnitID"] isEqualToString:@"Cups"]) {
                                    [cupsPastYearTotalArray addObject:measurementDict];
                                }
                            }
                        }
                    }
                }
         completionBlock(cupsPastYearTotalArray);

问题是我的数组“cupsPastYearTotalArray”有超过 500 个对象,所以我的项目内存使用量上升到 600 MB,然后崩溃。

管理所有这些数据以防止我的应用程序崩溃的最佳方式是什么?

最佳答案

您可以尝试使用 autoreleasepool block .

 [edcs getClinicalPatientDataWithDictionaryForPatientID:participant.participantNum withStartDate:pastDate withEndDate:currentDate completionBlock:^(NSDictionary *jsonDict) {
        if ([jsonDict[@"status"] isEqualToString:@"200"] && jsonDict[@"body"]) {
            NSMutableArray *body = [NSMutableArray  arrayWithObject:jsonDict[@"body"]];
            for (NSArray *jsonArray in body) {
                @autoreleasepool {
                    for (NSDictionary *measurementDict in jsonArray) {
                        @autoreleasepool {
                            if (![body.description containsString:@"<null>"] && measurementDict && [[measurementDict objectForKey:@"MeasureUnitID"] isEqualToString:@"Cups"]) {
                                [cupsPastYearTotalArray addObject:measurementDict];
                            }
                        }
                    }
                }
            }
        }
        completionBlock(cupsPastYearTotalArray)
    }];

关于ios - 由于 'GET' 请求多个对象的内存问题导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591818/

相关文章:

ios - Guard Malloc 导致 wacko 应用程序行为

ios - 仅当滚动内容到达边缘时才识别 UIScrollView 中的滑动手势

ios - Apple Watch 模拟器以通知 View 启动 watch 应用程序。如何在main中自动启动?

ios - 在 Storyboard预览 xcode 中获取 "xxx.storyboard is currently locked because it is a remote resource"

ios - 更换 App Store 中的现有应用程序需要哪些步骤?

objective-c - 在 iPhone 中带有购物车按钮的 Paypal

ios - 如何在没有自定义单元格的情况下将文本包装在 UITableViewCell 中

ios - 标准 UIButton isAccessibilityElement 默认返回 NO

ios - __strong 在 Objective C 中的用法示例

ios - 如何在 Xcode 7 上成功上传二进制文件而不会出现此错误?