How to load data from web-service while displaying

2019-02-27 00:30发布

I want to load some data from web at the starting of my app launch. I have set a splash screen and in didFinishLaunchingWithOptions I have set the sleep(10). Now I want that in this time interval my app call the web service and load the data but I am not able to do this. Please help me out or give any suggestion to do it.

5条回答
三岁会撩人
2楼-- · 2019-02-27 00:56

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-27 00:59

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.

查看更多
来,给爷笑一个
4楼-- · 2019-02-27 01:08

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...

:)

查看更多
冷血范
5楼-- · 2019-02-27 01:16

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.

查看更多
唯我独甜
6楼-- · 2019-02-27 01:19

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! :)

查看更多
登录 后发表回答