Question

In my iOS native app, I parse an XML file, parsing everything using a class called RSSEntry. It takes the date from pubDate and stores it as articleDate, an NSDate. After it parses everything, this code is ran:

- (void)requestFinished:(ASIHTTPRequest *)request {

    [_queue addOperationWithBlock:^{

        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
                                                               options:0 error:&error];
        if (doc == nil) {
            NSLog(@"Failed to parse %@", request.url);
        } else {

            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {

                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntry *entry1 = (RSSEntry *) a;
                        RSSEntry *entry2 = (RSSEntry *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                }

            }];

        }
    }];
    [self.refreshControl endRefreshing];

}

This adds each entry into the UITableView, sorted by date. However, some dates will be in the future. I would like to only add the entries with a pubDate of that day or earlier, eliminating future ones, until their date approaches. suggestions?

Was it helpful?

Solution

Add this method in your class

- (BOOL)isDate:(NSDate *)startDate earlierThanDate:(NSDate *)endDate
{
    if ([startDate compare:endDate] == NSOrderedAscending) {
        return YES;
    } else if ([startDate compare:endDate] == NSOrderedSame) {
        return YES;
    } else {
        return NO;
    }

}

Then before adding object to the array,check whether it is a future date or not with this method

- (void)requestFinished:(ASIHTTPRequest *)request {

    [_queue addOperationWithBlock:^{

        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
                                                               options:0 error:&error];
        if (doc == nil) {
            NSLog(@"Failed to parse %@", request.url);
        } else {

            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {
                    if ([self isDate:[NSDate date] earlierThanDate:entry.articleDate])  {

                        int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                           RSSEntry *entry1 = (RSSEntry *) a;
                           RSSEntry *entry2 = (RSSEntry *) b;
                           return [entry1.articleDate compare:entry2.articleDate];
                        }];

                          [_allEntries insertObject:entry atIndex:insertIdx];
                          [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                    }



                }

            }];

        }
    }];
    [self.refreshControl endRefreshing];

}

OTHER TIPS

A couple ideas come to mind:

  • When you parse the xml don't add the object to the final array if the date is in the future.

  • I assume your numberOfRowsInSection method is returning the number of elements in your array. If this is the case then find the index of the first item whose date is in the future and then return 1 less than that. This should work since your array is sorted. This would have the benefit of making it easy to reload the tableview and have rows that didn't make the cut previously to be shown with no additional work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top