Fabio Cardoso
Fabio Cardoso

Reputation: 1269

Parse.com app goes forward before i get the value I want (async) iOS

I use a query FindObjectsInBackgroundWithBlock (assync) to get a value from webservice... I want that value on the appdelegate before the app loads. I'm trying do it but he still load the app before check the webservices to get the value. How can I handle it? I already tried with a NSTimer..

I called this method in -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions :

-(void)checkUserVersion{
    PFQuery *query = [PFQuery queryWithClassName:@"sqliteversion"]; //1
    // NSNumber *n=databaseVersion;
    [query whereKey:@"user_version" equalTo:[NSNumber numberWithInt:[databaseVersion integerValue]]];//2
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {//4
        if (!error && [objects count]>0) {

            for (PFObject *object in objects) {
                valor1=[[object objectForKey:@"Value"]intValue];
                user_version=[NSNumber numberWithInt:valor1];


            }

        }
    }];
}

Upvotes: 0

Views: 1681

Answers (2)

Daddy
Daddy

Reputation: 9035

You may want to evaluate why it's necessary to have this information from Parse.com before launch. Also, it can mean many things to say "before launch", and there are many solutions that don't involve doing "everything" before the first view is attached to the window and shown.

That said...run the PFQuery fetch in the foreground. Doing this will block execution of the rest of your code, though. Instead of findObjectsInBackground just use the findObjects method of PFQuery. I would advise against doing this though, and instead try to find out a way to wait for the query to return while the app is already loaded. Maybe you could suspend user interaction but display a loading indicator or something?

Upvotes: 1

jsd
jsd

Reputation: 7693

You can't run any code in your app before it finishes launching. If the value is critical for your app to begin working you'll have to show a "waiting for data" message.

Upvotes: 1

Related Questions