Search code examples
iosnetwork-programmingpaginationsingletontableview

tableview memory management - singleton class


I am newbie on ios programming, so I have a question. I am working on an app, will continue to load data on internet, it's paging data, so when the user navigate to the next page, it will load the data of the page on the internet. I use a singleton class to make it, it works fine, but I had a question -

When the first page is arrival I save it to self.posts variable like - self.posts = dataA, and when the user go for the next page, it will change self.posts to dataB, like self.posts = dataB. my question is, if the dataA will be released by iOS automatically, or it's not? if it's not, how to deal with these garbage memory? You know it will load data page by page, if so many pages being loaded, it might be a problem......Thanks.

Sorry forget to tell you guys, the app is for iOS 3.x+, so I guess ARC is not available. Check this function, it will be called after the HTTP connection is done and will parse JSON to NSDictionary, each time it will load about 5 posts for a page, and next page is another 5 posts, so you know, the self.posts changed if it's another new HTTP networking.

- (void) getNextPostsFromJson:(NSData *)data
{
    NSError *theError = nil;

    NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&theError];

    if (dict == nil) {
        isValidJson = NO;
        httpStatus = HTTP_STATUS_FAILED;

        NSLog(@"json con - %@ %@",
              [theError localizedDescription],
              [[theError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);   
    } else {

        NSArray *keys = [dict allKeys];        

        if ([keys count] >= TOTAL_SECTIONS) {
            self.posts = dict;
        } else {
            self.posts = nil;
        }

        NSLog(@"posts = %@", self.posts);
        return;
    }
}

Solution

  • Toaster suggestion is right.

    If you use a property like the following.

    @property (nonatomic, retain) NSDictionary* posts;
    

    when you do

    self.posts = dataB;
    

    the old value referenced object is released for you.

    The setter synthesized by the compiler using @synthesize directive looks like this (pseudo code here):

    - (void)setPosts:(NSDictionary*)newPosts
    {
        if(newDict != posts) {
    
            [newPosts retain]; // retain the new value
            [posts release]; // release the old value
            posts = newPosts; // now posts reference the new value
        }
    }
    

    A simple suggestion for you.

    If you do self.posts = dataB you lose dataA. So when you came back, you need to perform the download again. So, what do you think to have a cache of downloaded data? For example create a NSMutableDictionary where each key is the page (the number of the page or whatever you like) and each value is the data (dataA, dataB and so on). Through it, you can avoid to download data each time. Maybe you can also set up a limit for this cache (say 5 data) to prevent memory issues.

    Hope it helps.