Question

I have a problem that I'm not able to solve by myself. I have to download data from my Web server before the user can see the View(UITableViewController), so at the time that the UITableView should be visible, it should have the content to show. The problem is, I start downloading the data in applicationDidFinishLaunching, I have NSLogs in viewDidLoad and in applicationDidFinishLaunching, the funny thing is that the NSLogs of the viewDidLoad are called before the NSLogs of the applicationDidFinishLaunching (those NSLogs are wrote just after all the data is downloaded).

Once i finish downloading the data, i have an NSMutableArray that i pass from the AppDelegate to the TableViewController, but the tableViewController appears totally empty because the ViewDidLoad is called before all the data has been downloaded.

Then I have 1 question: Where i should download all the data before the TableView is loaded?

Solution that I´ve been thinking about: I thought about creating another ViewController(It would be the main View) with an UIImageView that have the image " Loading... ": There I download all the data that I need and I store all the Data to an NSMutableArray. After the data is downloaded, I call the UITableViewController and i pass the NSMutableArray to the UITableViewController. I am sure this solution will work, but it has to be an easier way to solve this.

Was it helpful?

Solution

A good solution would be create a delegate method that tells your ViewController when the download finished. And on the implementation of the method, you call [yourTableView reloadData].

On the class you will download data (on YourDownloadClass.h):

@protocol WebServiceConsumerDelegate 

//@required
-(void) requestDidFinishSuccessfully:(NSArray*) resultArray;

@end

@interface YourDownloadClass : UIViewController

@property (strong, nonatomic) id <WebServiceConsumerDelegate> delegate;

And on YourDownloadClass.h just after you download data:

[self.delegate requestDidFinishSuccessfully:resultArray];

Now, go to your TableViewClass.h

#import "YourDownloadClass.h"

@interface TableViewClass : UITableViewController <WebServiceConsumerDelegate>

Finally, on TableViewClass.m:

-(void)viewDidLoad{
 [super viewDidLoad];

 YourDownloadClass* delegateClass = [YourDownloadClass alloc] init];

 // Or if you are downloading on AppDelegate:
 AppDelegate* delegateClass = [UIapplication sharedApplication];

 delegateClass.delegate = self;

}

Finally, implement the method:

-(void)requestDidFinishSuccessfully:(NSArray*) resultArray{

   //Get the objects of resultArray to use on your table view

   [yourTableView reloadData];

}

I hope it helps. :)

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