Start a new topic

KCSFileStore retaining a view controller?

I have a few different view controllers that use the method below to grab a logo image from the Kinvey File Store and display it in an imageView. I was noticing that after I implemented these calls, my apps memory usage was growing with every load of a view controller that used it. After some aggressive NSLogging, it looks like that if I use this method in a view controller, it doesn't get deallocated when it should. If I comment out this method, it deallocates normally. Any thoughts on why this is? I am using this in a few different VCs and the behavior is always the same. No call = normal deallocation. Call = retain cycle?



- (void)fetchAgencyLogo

{

NSString *filename = [NSString stringWithFormat:@"%@.png", self.agency._id];



[KCSFileStore downloadFileByName:filename completionBlock:^(NSArray *downloadedResources, NSError *error) {

if (error == nil && downloadedResources.count > 0) {

KCSFile* file = downloadedResources[0];

NSURL* fileURL = file.localURL;

UIImage* image = [UIImage imageWithContentsOfFile:[fileURL path]]; //note this blocks for awhile

NSLog(@"This is an image: %@", image);





} else if (error == nil && downloadedResources.count == 0){

NSLog(@"No file with that name you moron.");

} else {

NSLog(@"Error: %@", error);

}

} progressBlock:^(NSArray *objects, double percentComplete) {

NSLog(@"percent Comp: %.2f", percentComplete);

}];


Turns out this is caused by something I didn't include in my code above. I was using the returned image to set the image property on an imageView. It appears that my use of the self.imageView.image= was causing the retain cycle. Specicfically, the use of the "self" cell. In case anyone has this issue in the future, I used the following code to get access to the self,imageView property without causing a retain cycle.





- (void)fetchAgencyLogo

{

NSString *filename = [NSString stringWithFormat:@"%@.png", self.agency._id];



__block UIImageView *blockImageView = self.imageView;



[KCSFileStore downloadFileByName:filename completionBlock:^(NSArray *downloadedResources, NSError *error) {

if (error == nil && downloadedResources.count > 0) {

KCSFile* file = downloadedResources[0];

NSURL* fileURL = file.localURL;

UIImage* image = [UIImage imageWithContentsOfFile:[fileURL path]]; //note this blocks for awhile

NSLog(@"This is done");



//Get the main thread to update the UI

dispatch_async(dispatch_get_main_queue(), ^{

blockImageView.image = image;



});



} else if (error == nil && downloadedResources.count == 0){

//stuff

} else {

//error stuff

}

} progressBlock:nil];



}
Glad you got it resolved!
Login or Signup to post a comment