【问题标题】:App crash when trying to load a large amount off data into my app尝试将大量数据加载到我的应用程序时应用程序崩溃
【发布时间】:2015-02-01 07:27:50
【问题描述】:

我正在尝试将包含 40 000 多条记录的 JSON 加载到我的 Realm Database 中。这是我的功能

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc]init];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    NSArray *relations = [JSON copy];
    NSLog(@"COUNT SI %d",relations.count);

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *relations = [JSON copy];
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];

        for (NSDictionary *dict in relations) {
            Relation *relation = [[Relation alloc]init];

            relation.rel_address = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Address"]];
            relation.rel_balanceTotal = [[dict valueForKey:@"BalanceTotal"]doubleValue];
            relation.rel_bank_country_code = [NSString stringWithFormat:@"%@",[dict valueForKey:@"BankCountryCode"]];
            relation.rel_bank_number = [NSString stringWithFormat:@"%@",[dict valueForKey:@"BankNumber"]];
            relation.rel_city = [NSString stringWithFormat:@"%@",[dict valueForKey:@"City"]];
            relation.rel_city_id = [[dict valueForKey:@"CityId"]intValue];
            relation.rel_code = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Code"]];
            relation.rel_country = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Country"]];
            relation.rel_country_code = [NSString stringWithFormat:@"%@",[dict valueForKey:@"CountryCode"]];
            relation.rel_customerProspect = [NSString stringWithFormat:@"%@",[dict valueForKey:@"CustomerProspect"]];
            relation.rel_customerCode = [NSString stringWithFormat:@"%@",[dict valueForKey:@"CustomerProspectCode"]];
            relation.rel_email = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Email"]];
            relation.rel_expired_total = [[dict valueForKey:@"ExpiredTotal"]doubleValue];
            relation.rel_fax = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Fax"]];
            relation.rel_gsm = [NSString stringWithFormat:@"%@",[dict valueForKey:@"GSM"]];
            relation.rel_latitude = [[dict valueForKey:@"Latitude"]doubleValue];
            relation.rel_longitude = [[dict valueForKey:@"Longitude"]doubleValue];
            relation.rel_memo = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Memo"]];
            relation.rel_name = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Name"]];
            relation.rel_phone = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Phone"]];
            relation.rel_turnovertotal = [[dict valueForKey:@"TurnoverTotal"]doubleValue];
            relation.rel_vat_country_code = [NSString stringWithFormat:@"%@",[dict valueForKey:@"VATCountryCode"]];
            relation.rel_vat_number = [NSString stringWithFormat:@"%@",[dict valueForKey:@"VATNumber"]];
            relation.rel_website = [NSString stringWithFormat:@"%@",[dict valueForKey:@"Website"]];
            relation.rel_zipcode = [NSString stringWithFormat:@"%@",[dict valueForKey:@"ZipCode"]];
            [realm addObject:relation];
        }
        [realm commitWriteTransaction];
        compblock(YES);

    });


} failure:^( NSURLRequest *request ,NSHTTPURLResponse *response ,NSError *error , id JSON ){
    NSLog(@"error is %@",error);
}];
[operation start];

对于 10 000 个对象,一切正常。但是当我达到 40 000 时,我得到了这个错误:

Communications error: <OS_xpc_error: <error: 0x356dc614> { count = 1, contents =
    "XPCErrorDescription" => <string: 0x356dc86c> { length = 22, contents = "Connection interrupted" }
}>

谁能帮帮我?提前致谢!!

编辑

它在“COUNT SI”日志之前崩溃。所以我认为它与AFNetworking有关? 我还注意到它并没有在模拟器上崩溃......

【问题讨论】:

  • Woooooow - 不要这样做。添加分页,使用CoreData。不要一次加载这么多的数据 - 真的很糟糕
  • 数据从何而来?很可能,当您请求那么多数据时,服务器会花费大量时间来构建连接超时的响应。在类似的情况下,如果连接超时,我必须构建一个启发式来“调整”请求的大小。
  • Grzegorz Krukowski 是对的,您不应该一次将这么多数据加载到内存中,但我认为没有理由为此使用 Core Data over Realm。事实上,一旦插入数据,Realm 将使用更少的内存。 JSON 解析会占用大量内存。
  • 嗨,史蒂夫!你有没有想出任何解决方案?我遇到了同样的问题,但记录数量较少。
  • @Goti 目前我已经放弃使用核心数据,我现在使用 Realm (www.realm.io) 要快得多!!!

标签: ios objective-c json afnetworking realm


【解决方案1】:

在@autoreleasepool中包裹循环体

同样复制相同的 JSON 两次似乎是多余的。

【讨论】:

    【解决方案2】:

    这个问题与Realm无关。

    我正在尝试加载包含 40 000 多条记录的 JSON

    你的问题。 AFJSONRequestOperation 将尝试反序列化内存中的 JSON,您的应用将不再有任何可用内存,并将被终止。

    我还注意到它并没有在模拟器上崩溃...

    这是因为模拟器可以访问比 iOS 设备更多的内存。

    您应该找到减少网络请求大小的方法,或者通过一次请求更少的数据,或者使用比 JSON 字符串更少浪费的响应格式。

    【讨论】:

    • 好吧,我现在要在我的 JSON 中处理分页了!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多