Monday, July 18, 2022

[FIXED] How to reduce the initial time to show loader?

Issue

I am implementing loader in button action , but when i click on button it takes 3 to 5 sec to start loading. i am using two controller to implement loader- UIImage+animatedGIF.h to use gif image and MBProgressHUD.h to implement progress time. i don't know how to reduce the initial time that takes to start loader. I am using this code to implement loader-

NSURL *url = [[NSBundle mainBundle] URLForResource:@"loadingg" withExtension:@"gif"];

    self.loader.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];


    float progress = 0.0f;

    while (progress < 1.0f) {



        progress += 0.01f;

       // HUD.progress = progress;

        usleep(50000);


    }

    [NSTimer scheduledTimerWithTimeInterval:progress target:self selector:@selector(abcd) userInfo:nil repeats:NO];     

Solution

I must assume that usleep(50000) is not on the main thread.

If it is, there is your 5 secs delay. Sleeping the main thread does just that: wait. The right approach is an asynchronous load.

Read @bbum recommendation for when it's appropriate to invoke usleep: what is the difference among sleep() , usleep() & [NSThread sleepForTimeInterval:]?



Answered By - SwiftArchitect
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.