3

I am trying to implement a refresh-function. I have multiple types of operations populating my tableview, and I simply want to re-run the last operation.

I have this property:

 @property (nonatomic, strong) GetRouteOperation* refreshOperation;

Where the RouteOperation inherits from MKNetworkOperation, and it is a superclass of different types of RouteOperations, for example "Get routes in radius", "Get routes by cities", etc.

Im initializing the operation like this:

GetRoutesWithinDistanceOperation *operation = [[GetRoutesWithinDistanceOperation alloc] initOperation];

[self refreshRoutesWithOperation:operation];

Im running the operation like this:

- (void)refreshRoutesWithOperation:(GetRouteOperation *)operation
{

self.refreshOperation = [operation copyForRetry];

[operation getRoutesCompletionHandler:^(NSArray *routes) {
    self.routes = routes;
    [self.tableView reloadData];
} errorHandler:^(NSError *error) {

} notModifiedHandler:^{

}];
}

and it works the first time I fetch my data.

When I try to refresh the table like this:

    [self refreshRoutesWithOperation:self.refreshOperation];

I get an exception in this method for the MKNetworkOperation-class:

-(void) addCompletionHandler:(MKNKResponseBlock)response errorHandler:(MKNKResponseErrorBlock)error {

  if(response)
    [self.responseBlocks addObject:[response copy]];
  if(error)
    [self.errorBlocksType2 addObject:[error copy]];
}

this is the exception:

2013-08-30 14:00:32.211 AppName[3500:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x9960540'

Why?! Why can't I simply run the copied operation like the first time.

EDIT:

It seems as if the actual copyForRetry doesn't work for me. Or I am using it in a wrong way.

Even if I try to use the copied object directly after copying it, It crashes. This does not work; I get the same exception:

self.refreshOperation = [operation copyForRetry];

[self.refreshOperation getRoutesCompletionHandler:^(NSArray *routes) {
...
}

Anyone got a good solution for this? How to store the last operation for refreshing purposes?

Edit2: I try to use the NSObject method copy aswell, but still get the same problem. I guess there's something I'm missing with the copy-methods.

However I do not need to use this approach for a solution, I just need a smooth solution. Any ideas?

ullstrm
  • 9,812
  • 7
  • 52
  • 83
  • http://stackoverflow.com/questions/3220120/nsmutablearray-addobject-nsarrayi-addobject-unrecognized-selector-sent-t – iPatel Aug 30 '13 at 12:05
  • How are you declaring this variables `responseBlocks` and `errorBlocksType2`? – Raúl Juárez Sep 02 '13 at 21:18
  • It is declared by the MKNetworkOperation-framework. There's no problem with the actual networkoperation. The thing is that I cannot use my copied object afterwards. Perhaps there's a better way to solving the retry than copying the operation like I do. – ullstrm Sep 03 '13 at 06:05

1 Answers1

1

Looks like a bug in MKNetworkOperation.m (line 507):

https://github.com/MugunthKumar/MKNetworkKit/blob/master/MKNetworkKit/MKNetworkOperation.m

The NSMutableArray property responseBlock is copied with "copy" instead of "mutableCopy". The thinking of the author was probably that he can't call "mutableCopy" within a "copy" method but that doesn't make much sense as there is no immutable variant of the MKNetworkOperation class.

lassej
  • 6,256
  • 6
  • 26
  • 34