Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

iPhone Alert View - Pause Code?

Options
  • 14-11-2010 3:47pm
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    This is a question that I can't seem to get an answer to. I have a view that loads a table. However I've moved into object archiving and for development purposes I want to have an initial AlertView that asks if a user wants to use a saved database or download a fresh copy

    - (void)showDBAlert {
    	
    	UIActionSheet *alertDialogOpen;
    	alertDialogOpen = [[UIActionSheet alloc] initWithTitle:@"DownloadDB?" 
    					delegate:self
    					cancelButtonTitle:@"Use Saved" 
    					destructiveButtonTitle:@"Download DB" 
    										 otherButtonTitles:nil];
    	[alertDialogOpen showInView:self.view];
    }
    

    I'm using an ActionSheet in this instance. And I have implemented the protocol:
    @interface ClubTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate>
    


    Based on this I run this to check what button was pressed:
    -(void)actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    	
    	NSString *buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
    	
    	if ( [buttonTitle isEqualToString:@"Download DB"] ) {
    	
    		[self loadDataFromDB];
    		
    	}
    	
    	if ([buttonTitle isEqualToString:@"Use Saved"] ) {
    		
    		self.rows = [NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]];
    		
    		if (self.rows == nil) {
    			[self loadDataFromDB];
    		}
    					 
    		
    			
    	}
    }
    


    The problem is my code to build the table executes before the user has made there choice. This causes all sorts of havok. As a result, how can I pause the code until a user has made there choice?


Comments

  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    What sort of havok?
    could the naswer be as simple as to just hide the tableView until the user has made their selection the calling the [tableView reloadData]; method?

    I'm pretty sure the tableView has a mind of it's own and will reload when it feels like it.


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Thank you for the reply!

    Basically when the alertView pops up, the code is still executing. So the table gets built with null values. I know this from NSLog.

    Then when a user makes a selection, it loads up the data from the database, which causes a malloc error. Its a royal mess to be honest.

    The loadDB call looks like this:
    -(void)loadDataFromDB {
    	
    	NSLog(@"LoadDBCalled");
    	//NSLog(@"ViewhasLoaded");
    	
    	NSURL *url = [NSURL URLWithString:@"http://localhost:80/books.php"];
    	
    	NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
    	
    	NSLog(@"%@", jsonreturn); // Look at the console and you can see what the restults are
    	
    	NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    	NSError *error = nil;
    	
    	
    	// In "real" code you should surround this with try and catch
    	
    	NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    	
    	NSLog(@"Dict: %@",dict);
    	
    	rows = [dict objectForKey:@"users"];
    
    	[self archiveData];
    	
    	[url release];
    	[jsonreturn release];
    

    rows is a NSMutableArray that is used to build the table.

    The Archive bit looks like this:
    
    -(NSString *) archivePath {
    	NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    	return [docDir stringByAppendingPathComponent:@"storageData.dat"];
    }
    
    -(void)archiveData {
    	dataStorage = rows;
    	NSLog(@"Archive DONE");
    	[NSKeyedArchiver archiveRootObject:dataStorage toFile:[self archivePath]];
    }
    

    Where dataStorage is another NSMutableArray.


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    What is rows?
    You are assigning it an object in loadDataFromDB then casting it to an NSMutableArray in archiveData.


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Thank you for the replies. I managed to sort this. When building a table, just return zero for the number of cells until your ready to go. Then reloadData.


Advertisement