Application error while trying to load most of the data into my application

I am trying to load JSON

40,000+ records into mine 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];

      

Everything works fine 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 help me? Thanks in advance!

EDIT

Crash before "COUNT SI" magazine. So I think it has something to do with AFNetworking? Also I noticed that it did not crash on the simulator ...

+3


source to share


2 answers


This issue is not related to Realm.

I am trying to load JSON from 40,000+ records

There is your problem. AFJSONRequestOperation

will try to deserialize the JSON in memory and your application will no longer have available memory and will terminate.



Also I noticed that it did not crash on the simulator ...

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

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

+7


source


Wrapper outline body in @autoreleasepool



Also copying the same JSON twice seems like overkill.

+2


source







All Articles