Adding to a MutableArray during pagination

The default entries I get from an API call are 20. While getting data from the second page, my array deletes the first 20 entries and then lads the new entries in it. Thus the size of the array remains constant at 20, despite me setting it to be so. What am I doing wrong?

getData gets called with the updated url value for loading the next page,

Code:

- (void)getData: (NSURL *) url {
    
    NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:url];
    NSError *error;
    NSDictionary *allCourses = [NSJSONSerialization
                                       JSONObjectWithData:allCoursesData
                                       options:kNilOptions
                                       error:&error];
    if( error ) {
        NSLog(@"%@", [error localizedDescription]);
    }
    else {
        self.data = allCourses[@"articles"];
        NSLog(@"%@", self.data);
        self.totalEntries = allCourses[@"totalResults"];

    NSMutableArray *_titles = [NSMutableArray array];
    NSMutableArray *_authors = [NSMutableArray array];
    NSMutableArray *_content = [NSMutableArray array];
    NSMutableArray *_images = [NSMutableArray array];
    
    for ( NSDictionary *theArticle in self.data) {
        
        [_titles addObject:[NSString stringWithFormat:@"%@", theArticle[@"title"]]];
        [_authors addObject:[NSString stringWithFormat:@"%@", theArticle[@"author"]]];
        [_content addObject:[NSString stringWithFormat:@"%@", theArticle[@"content"]]];
        
        //image formatting
        if ([theArticle[@"urlToImage"] isEqual: @""]) {
            [_images addObject:[NSNull null]];
        } else if (theArticle[@"urlToImage"] == [NSNull null]) {
            [_images addObject:[NSNull null]];
        } else {
            NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: theArticle[@"urlToImage"]]];
            [_images addObject:imageData];
        }
    }
    self.titles = _titles;
    self.authors = _authors;
    self.content = _content;
    self.images = _images;
    
    NSLog(@"%@", self.titles);
    [self.cv reloadData];
    [spinner stopAnimating];
}

}

Hi! These are forums about the swift programming language, not obj-c, also you haven't said what is your problem

Oh, okay. I will keep that in mind.
Regarding my problem,
While getting data from the second page, my titles array deletes the first 20 entries and then adds the new entries in it. Thus the size of the array remains constant at 20, despite me setting it to be so.

Simply because you re-init the array every time you call getData

Can you please tell me how I can add to the array without creating re-initializing it every time?

You need to declare the array outside of the getData function and either pass it in or access it via the same class which holds function.

Instead of:

self.titles = _titles;
self.authors = _authors;
self.content = _content;
self.images = _images;

Do this

[self.titles addObjectsFromArray:_titles];
[self.authors addObjectsFromArray:_authors];
[self.content addObjectsFromArray:_content];
[self.images addObjectsFromArray:_images];

However, do you really need to be creating those temporary arrays? Why not just:

[self.titles addObject:[NSString stringWithFormat:@"%@", theArticle[@"title"]]];
[self.authors addObject:[NSString stringWithFormat:@"%@", theArticle[@"author"]]];
etc...
Terms of Service

Privacy Policy

Cookie Policy