Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:20:30+00:00 2026-06-06T10:20:30+00:00

Here is ALL my code for this specific view controller. As the title of

  • 0

Here is ALL my code for this specific view controller. As the title of this question states, I am seeing my UITableView loaded with the same 9 values over and over again. If I place a breakpoint in connectionDidFinishLoading and wait one second I only see the data rendered to the table view once. I have tried all sorts of things to solve this; help is very much appreciated.

@implementation MasterViewController

@synthesize response;

NSMutableData* receivedData;
NSString* hostName;
NSInteger portNumber = 9999;
NSMutableDictionary* dictionary;
NSInteger maxRetryCount = 5;
int count = 0;

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {    
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Succeeded! Received %d bytes of data",[data length]);
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
receivedData = nil;
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];

if ([result isKindOfClass:[NSArray class]]) {

    for (NSArray *item in result) {
        NSArray *category = [item valueForKey:@"CategoryName"];
        [dataArray addObject:category];
    }
}
else {
    NSDictionary *jsonDictionary = (NSDictionary *)result;

    for(NSDictionary *item in jsonDictionary)
        NSLog(@"Item: %@", item);
}

connection = nil;
[self.tableView reloadData];

NSLog(@"Finished");
}

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return YES;
}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
    return YES;
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    NSString* username = @"test";
    NSString* password = @"testPwd";
    NSLog(@"Attempting connection with username: %@", username);
    NSMutableString *loginString = (NSMutableString*)[@"" stringByAppendingFormat:@"%@:%@", username, password];
    NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", loginString];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%i%@", hostName, portNumber, @"/api/categories"]];
    NSLog(@"URL: %@", url);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url
                                                       cachePolicy:       NSURLRequestReloadIgnoringLocalCacheData  
                                                   timeoutInterval: 1.0]; 
    [request addValue:authHeader forHTTPHeaderField:@"Authorization"];
    [NSURLConnection connectionWithRequest:request delegate:self];

    if ([challenge previousFailureCount] <= maxRetryCount) {
    NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 

    } else {
    [[challenge sender] cancelAuthenticationChallenge:challenge];
    NSLog(@"Failure count %d",[challenge previousFailureCount]);
    }
    }

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
self.navigationItem.rightBarButtonItem = addButton;

self.tableView.delegate = self;
self.tableView.dataSource = self;
}

- (void)addNewItem
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Product" 
                                                 message:@"Enter the new product name" 
                                                delegate:self 
                                       cancelButtonTitle:@"Cancel" 
                                       otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;

[alert show];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

- (void)insertNewObject:(id)sender
{
if (!_objects) {
    _objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) 
{
    [dataArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} 
else if (editingStyle == UITableViewCellEditingStyleInsert) 
{

}
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *object = [dataArray objectAtIndex:indexPath.row];
    [[segue destinationViewController] setDetailItem:object];
}
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];

[dataArray addObject:textField.text];
[self.tableView reloadData];

NSLog(@"Plain text input: %@",textField.text);
NSLog(@"Alert View dismissed with button at index %d",buttonIndex);
}

- (void)viewWillAppear:(BOOL)animated {
self.title = @"Categories";

receivedData = [[NSMutableData alloc] init];

hostName = [[NSString alloc] initWithString:@"12.234.56.123"];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%i%@", hostName, portNumber, @"/api/categories"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 1.0]; 

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

if (connection) {

} else {

}

dataArray = [[NSMutableArray alloc] init];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:20:32+00:00Added an answer on June 6, 2026 at 10:20 am

    willSendRequestForAuthenticaiton is a delegate method. Yet inside your delegate method, you are generating a brand new URL request, which in turn gets handled by the willSendRequest delegate, which generates another request, etc, which continually populates your dataArray with data.

    Get rid of the url request inside the delegate method and it should fix things for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I coded, global mini form validation for jquery. This here; This code, All form
enter code here Hi All, I have a simple windows service application that connects
Here is the code: <?php //Starting session session_start(); //Includes mass includes containing all the
Here is te code: <?php //Starting session session_start(); //Includes mass includes containing all the
Here is an example of my code in a DAL. All calls to the
Hey all. Here is the scenario. I have the below code, and I'm needing
It is Hallowe'en after all. Here's the problem: I'm maintaining some old-ish J2EE code,
Actually this is a more specific version of a question I posted in past
This is my first question here on stackoverflow, so I hope that I am
So this is probably a fairly easy question to answer but here goes anyway.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.