尝试将大量数据加载到我的应用程序时,应用程序崩溃 [英] App crash when trying to load a large amount off data into my app

查看:118
本文介绍了尝试将大量数据加载到我的应用程序时,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JSON 的40 000多条记录加载到我的领域数据库中。这是我的函数

I am trying to load a JSON of 40 000+ records into my Realm Database. Here is my function

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时,我收到此错误:

Everything works oké for 10 000 objects. But when I go to 40 000, I get this error:

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

任何人都可以帮助我吗?提前致谢!!

Can anyone please help me? Thanks in advance !!

编辑

它在COUNT SI之前崩溃记录。所以我认为它与AFNetworking有关?
另外我注意到它没有在模拟器上崩溃...

It crashes before the "COUNT SI" log. So I think it has something to do with AFNetworking? Also I noticed that it does not crashed on simulator...

推荐答案

这个问题与Realm无关。

This problem is unrelated to Realm.


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

I am trying to load a JSON of 40 000+ records

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

There's your problem. AFJSONRequestOperation will attempt to deserialize the JSON in memory and your app will no longer have any available memory, and will get terminated.


另外我注意到它没有在模拟器上崩溃......

Also I noticed that it does not crashed on simulator...

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

This is because the simulator has access to much more memory than an iOS device.

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

You should find ways to reduce the size of your network requests, either by requesting less data at a time or using a less wasteful response format than JSON strings.

这篇关于尝试将大量数据加载到我的应用程序时,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆