How to load data from web-service while displaying splash screen?

荒凉一梦 提交于 2019-12-02 00:45:30

During sleep, the thread (in this case, the main thread) is unable to do anything.

I would recommend you simply show the splash screen, start loading the data and hide the splash screen once all the data has been loaded.

Big Question!

First of all, don't make the Main thread sleep, nothing would work there, so it is just wasting time.

Instead, Setup something like an Updating Page with constantly running UIScrollView, Which would disappear only when your data has been fetched.

Use the delegate for the webservice, through which you would call a function in AppDelegate to remove the Loader View and add HOMEPAGE, when data has been fetched.

Something like,

this is just an example...

- (void) webserviceDidFinishLoading  //write in appdelegate.m
{
      [self.activityIndicatorView removeFromSuperView];
      self.window.rootController = self.homeViewController;
}

Hope this helps! :)

hey mate see bellow code..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        splashView = [[UIImageView alloc] initWithFrame:iphoneFrame];
        splashView.image = [UIImage imageNamed:@"Default"];
        [self.window addSubview:splashView];
        //// load you web-service here and get data. After 2 sec iphone rootview controller will display
        [self performSelector:@selector(loadViewIphone) withObject:nil afterDelay:2.0];
     }
}

-(void)loadViewIphone 
{
    [splashView removeFromSuperview];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFade];
    [animation setDuration:0.5];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[self.window layer] addAnimation:animation forKey:kAnimationKey];
}

i hope this help you...

:)

Sleeping the main thread and showing the splash screen for long time is not a good idea. You can achieve the same thing by following a simple trick. I think in your case service is being called from the very first view controller after hiding splash screen. So you can create a modal view containing the same image like splash screen. And display like following :

SLSDummySplash *dummySplash = [self.storyboard instantiateViewControllerWithIdentifier:@"splashId"];
[self presentViewController:dummySplash animated:NO completion:nil];

When you are done with service calling / long loading event just dismiss the modal view.

Please see here: https://github.com/k06a/LaunchScreenViewController.

Display splash view controller right before the app's first view controller appears. When you are done loading data from web or initializing, dismiss the splash view controller.

Sleeping or performing a selector after a specific time are not the correct approaches because you never know how much to wait depending upon poor internet connectivity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!