簡體   English   中英

來回查詢時,iOS應用程序堆積內存

[英]iOS app pilling memory while segueing back and forth

在我的iOS應用中,當我使用系統內存將其從viewController A固定到B時可以。

但是問題是當我從viewController A到B依次從A依次回到A再到B時,這會不斷堆積越來越多的內存。

回到視圖A時為什么不釋放所有已用內存。

我正在使用自動釋放池。 而且我正在設置不再需要的變量。

注意:我在View B中使用GCD,但我也在GCD中釋放內存。

我不知道為什么會這樣。 還是有一種方法可以完全卸載視圖B所使用的所有資源?

更新:

我沒有從B切換回A。我只是在使用“后退”按鈕。

這是我的觀點

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

@autoreleasepool {

PhotosCollectionViewController *photoCollection=[segue destinationViewController];
NSIndexPath *path=[self.tableView indexPathForSelectedRow];

// get the cell tag for the selected row
NSIndexPath *path1=[NSIndexPath indexPathForRow:[path row] inSection:0];

UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:path1];

[photoCollection setPassedID:[[elements objectAtIndex:path1.row] objectAtIndex:0]];
// set the title of the next view controller
[photoCollection setTitle:cell.textLabel.text];

photoCollection=nil;
path=nil;
path1=nil;

}

}

編輯

我在viewDidLoad中的視圖b中使用的長時間運行的過程

photoQueue=dispatch_queue_create("com.prepare.photos", nil);
dispatch_async(photoQueue, ^{

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(goToBackground)
                                                 name:UIApplicationWillResignActiveNotification object:nil];


display_photos=[[NSMutableArray alloc] init];




// iterate through photos array and create MWPhoto object out of it

// init ns cache
//myCache=[[NSCache alloc] init];

    int j=0;

    for(j=0;j<[photos count];j++){
        //NSLog(@"filename %@",[[photos objectAtIndex:i] objectAtIndex:0]);

        MWPhoto *mw=[MWPhoto photoWithFilePath:[self.documentsDirectory stringByAppendingPathComponent:[[photos objectAtIndex:j] objectAtIndex:0]]];

        [display_photos addObject:mw];
        mw=nil;


    }

    //dispatch_release(photoQueue);

});

回答我自己的問題。 它堆積內存的原因是,當我從sqlite數據庫檢索某些數據時,我關閉了sqlite連接。 因此,鎖定到新視圖將重新打開數據庫連接,這將導致過多的內存使用。

因此,最重要的是,不要關閉sqlite數據庫連接。

Segues加載視圖控制器的新實例; 因此,當使用segue將其從窗體視圖控制器A導航到B時,它是有意義的。

A_1 -> B_1

但是,如果使用segue從B導航到A,則將創建視圖控制器A的新實例,而不是返回到視圖控制器A的先前實例。

A_1-> B_1-> A_2

來回吵鬧會導致以下結果:

A_1-> B_1-> A_2-> B_2-> A_3-> ...

理想情況下,您想要的是:

A_1-> B_1-> A_1

(從B_1返回A_1)

為此,您可能需要對從視圖控制器B導航到A的按鈕使用此方法。

- (IBAction)backButton:(UIButton *)sender
    {
        [self dismissViewControllerAnimated:YES completion:^(void){
            NSLog(@"View controller dismissed");
            // things to do after dismissing
        }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM