1

I use AFNetworking to create request with NSDictionary *parameters. One of my key value pairs are empty arrays:

NSMutableArray * array = [NSMutableArray new];

When I had created such example of parameters:

parameters = @{"ids": array, "name": "someName"};

Server received:

parameters = @{"name": "someName"};

Why the keys with empty arrays were removed?

After a long digging it turned out that the problem is with NSMutableArray's method.

AFNetworking uses at some moment NSMutableArray's method addObjectsFromArray:. It is not mentioned in Apple documentation, but this method adds nothing if array is empty.

When I replace my empty array with NSNull() or array with one element NSNull() the server do not receive it at all.

Anyone know how is it happening, or how to solve that problem?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

3 Answers3

0

I had a similar issue earlier in which I had to send an empty array to the server. This is what I had to do in order for the server to accept my request:

NSNull *nullValue = [NSNull null];
NSArray *arrayWithNull = [NSArray arrayWithObject:nullValue];
[payload setObject:arrayWithNull forKey:@"ids"];

In my case, the server returns this response:

ids =     (
    ""
);

The code used here I found in another SO question, found here: some confusions about [NSNull null],nil [duplicate]

Community
  • 1
  • 1
Andy Shephard
  • 1,726
  • 17
  • 26
  • Can you check what information is being sent to the server? You can normally do this in the debugger, by grabbing the HTTPBody from NSMutableURLRequest: po [[NSString alloc] initWithData:request.HTTPBody encoding:4] Just make sure that within the payload being sent to the server, that you have a URL parameter called ids[] – Andy Shephard Mar 19 '15 at 00:05
0

you can try with assign capacity to NSArray while you allocating memory to it

myArray = [[NSMutableArray alloc] initWithCapacity:10]; 
bLacK hoLE
  • 781
  • 1
  • 7
  • 20
0

This does seem to be a problem with the way AFNetworking handles things. I have made a fix for this within the AFNetworking library and have opened a pull request here:

https://github.com/AFNetworking/AFNetworking/commit/642158bac875362314c7db18a73d5a0e2137fd98

I hope it gets approved but in the meantime feel free to use my implementation by modifying your own fork of AFNetworking.

stephen
  • 1,617
  • 3
  • 20
  • 27