Search code examples
iphoneobjective-casynchronouspreloadappdelegate

iOS: How to Preload Data before Display


In an IPhone app I am making, I have an initialViewController

(1) with a button. Once the button is clicked, it segues to another View Controller

(2), which at that point loads data from a CoreData File and displays it to the user.

My issue is that there is a small delay between the loading of (2) and the actual Display of the data. That is of course because the data takes a little moment to be loaded. I am doing it asynchronously, and my goal is to never show a spinning wheel or a loading screen (more user friendly).

What I want to do is to "preload" the data at (1), not at (2), that way data should have already been loaded by the time (2) loads and should be displayed immediately. I know how to load the data at (1), but I have no idea how to easily pass it along to (2). I can't do it with a segue because my app is actually a bit more complicated than the description above and it's a hassle to do it via segue.

I have heard it is possible to use the "AppDelegate" but as I am new to programming I have no idea how to use it effectively. The online classes I've been following do not give very clear insight on how to use it, so I'm a bit lost.


Solution

  • After seeing your comment on Paras's post, I have a better solution for you:

    Create a subclass of NSObject called fileLoader

    //in fileLoader.h
    @interface fileLoader : NSObject {
    }
    +(fileLoader *)sharedInstance;
    +(void)createSharedInstance;
    //add functions and variables to load, store data for use in another class
    @end
    

    Then,

    //in fileLoader.m
    @implementation fileLoader
    static id _instance;
    
    
    +(void)createSharedInstance{
        _instance = [[fileManager alloc] init];
    }
    +(fileManager *)sharedInstance{
        return _instance;
    }
    //other functions for storing, retrieving, loading, standard init function, etc.
    

    @end Now you can call [fileManager createSharedInstance] to instantiate a file manager that you can use from anywhere by calling functions on [fileManager sharedInstance].