Kishore Kumar
Kishore Kumar

Reputation: 4375

View freezes for few seconds while accessing singleton class

This is the method I am using

 +(dataHandler *)getDataHandler
    {

        dispatch_once(&internalOnceToken,^{
            internalInstance = [[dataHandler alloc] init];
            if(internalInstance) {
                NSLog(@"Internal instance created: %@", internalInstance);
            }
        });
        if(internalOnceToken == -1)
        {
            NSLog(@"Internal instance exists: %@", internalInstance);
        }
        return internalInstance;

    }

I tried with dispatch_asyn but its not a static allocation ,In my home screen I have the access for all the data and stored in my singleton class.

But when I try to go from one view controller to home screen its get hang for few seconds how to overcome this?

Calling Singleton method:

 -(void)responseFunction:(NSMutableDictionary *)response
    {
            BOOL sucess;
            sucess =[[response objectForKey:@"sucess"]boolValue];
            NSLog(@"response Method%@",response);
            NSString *subimages;

            if(!sucess)
            {
                //storing response data to singleton class.
             }
         [self datahandlers];//here i am calling singleton.
    }

After storing i am calling singleton class in view did load method

if([file.dicCategoryDetails count]!=0&&[file.dicProductDetails count]!=0)
    {
        [self datahandlers];
    }

This is data handler method

-(void)datahandlers
{
    for(NSDictionary *diccategory in file.categoryArr)
    {
        NSMutableDictionary *dicparsing=[[NSMutableDictionary alloc]init];
        [dicparsing setObject:[diccategory objectForKey:@"category"] forKey:@"category"];
        [dicparsing setObject:[diccategory objectForKey:@"name"] forKey:@"name"];
        [dicparsing setObject:[diccategory objectForKey:@"image"] forKey:@"image"];
        [dicparsing setObject:[diccategory objectForKey:@"subcategory"] forKey:@"subcategory"];
        [ArrName addObject:dicparsing];

    }
    NSLog(@"inside data handler%@",ArrName);
    [collectionview reloadData];
    [self scrollView];

}

In view did load

 file= [dataHandler getDataHandler];

Upvotes: 0

Views: 97

Answers (2)

gnasher729
gnasher729

Reputation: 52592

If creating internalInstance takes measurable time, then your main thread will stall if it needs it, unless you change your code so that internalInstance isn't needed. It could help to call getDataHandler from a background thread as the very first thing when you launch the application, but no call to getDataHandler will return before internalInstance is created.

"call getDataHandler from a background thread":

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [SomeClass getDataHandler];
});

inside application:didFinishLaunching: or even earlier.

Upvotes: 0

Kishore Kumar
Kishore Kumar

Reputation: 4375

Finally i have done this just using dispatch_async(dispatch_get_main_queue(),^{})

 if([file.dicCategoryDetails count]!=0&&[file.dicProductDetails count]!=0)
    {
        dispatch_async(dispatch_get_main_queue(), ^{

                   [self datahandlers];
                });

    }

Upvotes: 1

Related Questions