I'm struggling to figure out a solution to my problem,
I have a Download class which handles calls to my api, these calls are added to a NSOperationQueue. Each call is assigned a completed and failed notifier which post's when the call completes or fails. I can then handle the completion/failure of a request in my view controller gracefully.
The problem i'm having is this, what is the correct way to alloc/init/release my download class. My first approach was like so:
alloc init a new instance of the download class every time i need to run a request, i can then have a unique instance of the class with unique complete and failed notifiers and other params as i so wish. The problem i had with this approach is when/how to release the object. I cannot simply call the fetch request and then release the object within the same call, as the download call has queue's to complete and notifications to post, i know when the instance of the download class has finished it's calls because of the notification, i just don't know the correct way to implement it's release from another function. e.g:
-(void)downloadLists:(int)page featured:(BOOL)featured {
NSMutableDictionary *postValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:page],@"page",@"false",@"is_featured", nil];
if(featured){
[postValues setValue:@"true" forKey:@"is_featured"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *destination = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"lists_%i.json",page]];
Download *download = [[Download alloc] init];
[download setCompleteNotifier:@"listsComplete"];
[download setFailedNotifier:@"listsFailed"];
[download downloadPOST:[NSURL URLWithString:@"http://blahblah"] values:postValues destination:destination];
}
Then where do i release download, and ensure i'm releasing the correct instance, downloadLists may be called (n) amount of times in quick succession.
My other approach was to use a singleton of Download, which was great until i needed to add userinfo to the notifications, which of course became jumbled up by the singleton class being called from different places.
Any help will be greatly appreciated, here is the downloadPOST function for your reference:
-(void)downloadPOST:(NSURL *)path values:(NSDictionary *)keyValues destination:(NSString *)destination {
ASIFormDataRequest *formRequest = [ASIFormDataRequest requestWithURL:path];
for(id key in keyValues){
[formRequest setPostValue:[keyValues objectForKey:key] forKey:key];
}
[formRequest setDownloadDestinationPath:destination];
[formRequest setDelegate:self];
[formRequest setDidFinishSelector:@selector(requestDone:)];
[formRequest setDidFailSelector:@selector(requestWentWrong:)];
[queue addOperation:formRequest];
}