Saturday, 24 August 2013

Stop deallocation of instance variable I need to stay around (but not worthy of being a property)

Stop deallocation of instance variable I need to stay around (but not
worthy of being a property)

I'm currently making block based wrappers around some methods of the
Dropbox, Google Drive, and Box.com SDKs. I've successfully wrapped upload
functionality into all three, and they all work nicely. The wrappers have
a "common" interface that conform to a universal spec among the three
services, which is why I'm doing this.
Dropbox is the only one that runs on delegate callbacks. I have no issues
with the upload classes. However, I'm trying to implement some download
classes in the same manner. I'm running into an issue so far that my
returned instances are getting immediately deallocated before any of the
delegate methods in the wrapper are called. First, check out the method
structure of the _working_ "Upload" class:
+(id)uploaderWithPaths:(NSMutableDictionary *)paths
progress:(JAUploadProgressBlock)progBlock
completed:(JAUploadCompletedBlock)compBlock
failed:(JAUploadFailedBlock)failBlock;
-(void)upload;
//in .m
return [[[self class] alloc]initWithPaths:paths
progress:progBlock
completed:compBlock
failed:failBlock];
-(void)upload {
//setup Dropbox Rest Client
//configure delegate, etc
[dbRestClient doUpload...];
}
//delegate stuff
..onProgress {
self.progressBlock(progress);
..onComplete {
self.completedBlock(info);
..onFailed {
self.failedBlock(error);
Thus, instantiating and performing an upload goes like this:
JAUploadProgressBlock progress = ^(CGFloat percentage) {
NSLog(@"%.2f",percentage); }
JAUploadCompletedBlock completed = ^(NSMutableDictionary *info) {
NSLog(@"Success: %@",info); }
JAUploadFailedBlock failed = ^(NSError *error) {
NSLog(@"Failed: %@",[error localizedDescription];
JAUploadWrapperDropbox *uploader = [JAUploadWrapperDropbox
uploaderWithPaths:paths
progress:progress
completed:completed
failed:failed];
[uploader upload];
In the upload wrapper class, the instance does not get deallocated
immediately, the delegate messages inside the wrapper get called, my block
callbacks fire, and everything goes as planned.
However, my Download wrapper class, which is modeled exactly like the
upload wrapper, gets deallocated before any of the delegate methods in the
wrapper are called.
This class is not conducive to being a property, because it's not suited
for re-use. It's really meant to be used once or as one offs. Using a
strong property fixes the issue, but like I said, I don't need to use a
property for my upload wrapper class.
What am I doing wrong?
You can check this project out at
https://github.com/justinxxvii/JAOperationWrapper At this time I have not
made a commit with any download wrappers.

No comments:

Post a Comment