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?