0

I am developing an app for my website that has an RSS Feed with a parser. I made a refresh button to look for new posts and it works. But now, I want it to display a UIAlertView that says "No posts found" if there were no new posts found.

This is my refresh button

- (IBAction)refreshButton:(UIBarButtonItem *)sender
{

    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc] init];

    // Construct a URL that will ask the service for what you want -
    // Note we can concatenate literal strings together on multiple lines in this way it
    // results in a single NSString instance
    NSURL *url = [NSURL URLWithString:
                  @"http://sephardijews.com/feed/"];

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Creating a connecting that will exchange this request for the data from the URL we specifed
    connection = [[NSURLConnection alloc] initWithRequest:req
                                                 delegate:self
                                         startImmediately:YES];

    [[self tableView] reloadData];
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);

}

How could I do this? Something with an if statement right?

The refresh button:

- (IBAction)refreshButton:(UIBarButtonItem *)sender
{

    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc] init];

    // Construct a URL that will ask the service for what you want -
    // Note we can concatenate literal strings together on multiple lines in this way it
    // results in a single NSString instance
    NSURL *url = [NSURL URLWithString:
                  @"http://sephardijews.com/feed/"];

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Creating a connecting that will exchange this request for the data from the URL we specifed
    connection = [[NSURLConnection alloc] initWithRequest:req
                                                 delegate:self
                                         startImmediately:YES];

    [[self tableView] reloadData];
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);

    if ([[self tableView] numberOfRowsInSection:0] > someNumberVariableForLastCount) {
        someNumberVariableForLastCount = [[self tableView] numberOfRowsInSection:0];
        [[self tableView] reloadData];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No New Posts" message:@"There were no new posts found" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [alert show];
    }
}
Daniel
  • 23,129
  • 12
  • 109
  • 154
jakife
  • 137
  • 1
  • 10

2 Answers2

0

Your reloadData call should occur in connectionDidFinishLoading, not right after you start the connection (it is an asynchronous network call, the table will be reloaded before anything is even fetched). The alert should be kicked off there too. You need to implement all the connection delegate stuff, hopefully you've done that. If not, basic example here.

Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
0

This will compare the current row count in a section you specify to a variable for the last count:

  if ([[self tableView] numberOfRowsInSection:0] > someNumberVariableForLastCount) {
            someNumberVariableForLastCount = [[self tableView] numberOfRowsInSection:0];
            [[self tableView] reloadData];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"no new items" message:@"OH NO!!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
            [alert show];
        }
jakife
  • 137
  • 1
  • 10
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • what should the last count be? – jakife Aug 17 '12 at 20:00
  • You'll want to declare it in your .h, but then probably set it initially to 0 in your `viewDidLoad` like so: `int someNumberVariableForLastCount = 0;` – Mick MacCallum Aug 17 '12 at 20:04
  • @YuvalMarcus If my answer has helped you please remember to mark it as correct :) – Mick MacCallum Aug 17 '12 at 20:05
  • 1
    I updated it because at first there was an error. It all works now! THANK YOU – jakife Aug 17 '12 at 20:12
  • Actually there is a small bug. When I click the refresh button and there are no new posts it doesn't show the alert. Only on the second time it does it. Do u know how to fix this? – jakife Aug 17 '12 at 20:18
  • @YuvalMarcus Are you setting `someNumberVariableForLastCount` to zero anywhere? – Mick MacCallum Aug 17 '12 at 20:22
  • int someNumberVariableForLastCount = 0; I did that in the viewdidload – jakife Aug 17 '12 at 20:25
  • and this in the .h @interface MasterViewController : UITableViewController { NSURLConnection *connection; NSMutableData *xmlData; int someNumberVariableForLastCount; RSSChannel *channel; } – jakife Aug 17 '12 at 20:26
  • Alright, good. Now sense you have declared "int someNumberVariableForLastCount;" in your .h, change viewDidLoad to say "someNumberVariableForLastCount = 0;" without "int" in front of it – Mick MacCallum Aug 17 '12 at 20:27
  • And on the 3rd time (time after the first time the window appears) i click the refresh button it will refresh and add the new post, but it will display no posts found when posts have actually been found – jakife Aug 17 '12 at 20:34
  • @YuvalMarcus I'm sorry to say I don't have all the answers, but the code I've provided should be enough to send you in the right direction. Good luck! – Mick MacCallum Aug 17 '12 at 20:38