Reputation: 1007
I have checked all related questions and answers both on Stackoverflow and internet but i came to no conclusion. I am developing an ipad app, and what it does is making request to my web server download pdf file as NSData and showing it in UIWebView. The problem is even though the app(closes) segues from pdfviewer to mainviewcontroller, memory is not released thus increasing memory usage all the time. It starts with 14 mb and then every time i segue to pdfviewer from main controller it adds up. Since i'm using arc i can't release it. I am also clearing cache.
The Mainview controller.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showpdf"]){
PdfViewer *pdfviewer=(PdfViewer*)[segue destinationViewController];
pdfviewer.ReportID=TheReportID;
pdfviewer.TabID=_TabID;
}
}
PdfViewer.h
@property (strong, nonatomic) IBOutlet UIWebView *MainWebView;
PdfViewer.m
-(void)viewDidLoad
{
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_MainWebView loadRequest:request];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"ShowMain5"]){
MainViewController *mv=(MainViewController*)[segue destinationViewController];
mv.TabID=_TabID;
}
}
-(void)dealloc{
NSLog(@"test");
}
i also tried to add UIWebview programmatically instead of IBoutlet but still couldn't solve the problem.
Programmatically( alternative )
@implementation PdfViewer
UIWebView *webview;
- (void)viewDidLoad
{
webview=[[UIWebView alloc]initWithFrame:CGRectMake(35, 35, 768,959)];
[webview loadRequest:request];
[self.view addSubview:webview];
}
When user press back button all i do is go back to mainviewcontroller modally.All segues are modal FYI.
As i understood that pdfviewer is not calling dealloc when it segues back to mainviewcontroller i checked if anyone had similar issue with viewcontrollers before and i found this question UIViewController -dealloc method not called now i think memory stays at the same level, but now it gives pdfviewer is not in the window hierarchy error.
Upvotes: 3
Views: 2346
Reputation: 56
Set webview delegate to nil while segues from pdfviewer to mainviewcontroller
[_MainWebView setDelegate:nil];
--- edit
[_MainWebView removeFromSuperView];
Upvotes: 0
Reputation: 16271
For my experience with UIWebView[s] the solution was to have a singleton instance for it, so at least this basic interface and properties
// MXMWebView.h
@interface MXMWebView : NSObject <UIWebViewDelegate, UIScrollViewDelegate> {
}
+ (instancetype)sharedInstance;
@property(nonatomic, strong) UIWebView *webView;
and
//MXMWebView.m
@implementation MXMWebView
@synthesize webView;
@synthesize delegate;
#pragma mark - Singleton Methods
+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
Upvotes: 2
Reputation: 1007
As i understood that pdfviewer is not calling dealloc when it segues back to mainviewcontroller. i checked if anyone had similar issue with viewcontrollers before and i found this question UIViewController -dealloc method not called. So i added
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.view.window.rootViewController=self;
}
to both mainviewcontroller.m and pdfviewer.m and also
-(void)dealloc{
[_MainWebView loadHTMLString:@"" baseURL:nil];
[_MainWebView stopLoading];
[_MainWebView setDelegate:nil];
[_MainWebView removeFromSuperview];
[[NSURLCache sharedURLCache]removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
method to pdfViewer.m and it solved the problem.Thanks everyone for their contribution.
Upvotes: 1