r/ObjectiveC • • Nov 04 '19

Adding to a MutableArry 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];
    }
}
0 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/NSCFType Nov 04 '19

The properties are nil when they're being sent -addObjectsFromArray:.

FYI - Your API key is in the log output. Might want to revoke and issue.