Parse Plugin and Execution Order

Hello!

I am new to the Parse plugin and am having some trouble with it, when I run the following code:

local parse = require( 'plugin.parse' ) parse.config:applicationId( "myId" ) parse.config:restApiKey( "myKey" ) function loginSuccess() local i = 0 ; while ( i \< 10 ) do i = i + 1 print( i ) end end local j = 10 ; print ( "Login attempt.") function networkCall() local req2 = parse.request( parse.User.login ) :options( { username = "myUserName", password = "myPassword" } ) :response(function( ok, res, info ) if not ok then print('err', res ) else if info.status == 200 then loginSuccess() end end end) end networkCall() while ( j \< 20 ) do j = j + 1 print( j ) end

It runs the while ( j < 20 ) do loop before I get the results from the networkCall function.  Any ideas?

Thanks!

Generally network operations are asynchronous. That means because they take an undetermined amount of time to run. If you hit a slow web site and we waited on the network operation to complete, your app would pause for several seconds while the network operation completes. So our network calls won’t block and run in the background. When they complete then your response function triggers and you get your results.

The remaining block of code:

while ( j \< 20 ) do j = j + 1 print( j ) end

In machine code, this breaks down to three machine instructions for the loop plus the amount of code to print the value of J out. So you’re looking at 60 instructions (plus calling print 20 times).  The older iPhone 4 could execute 2,000,000 instructions in a second. Your loop is only a few thousand instructions at best. The loop completes very quickly, probably faster than the plugin can event get the socket open to Parse.com.

Hi,

As Rob mentioned, the network call is asynchronous. Have a look at the network.request Corona docs to understand what is happening behind the scenes.

This isn’t so much an issue with the plugin, but how you decide to architect your application code.

Cheers.

Generally network operations are asynchronous. That means because they take an undetermined amount of time to run. If you hit a slow web site and we waited on the network operation to complete, your app would pause for several seconds while the network operation completes. So our network calls won’t block and run in the background. When they complete then your response function triggers and you get your results.

The remaining block of code:

while ( j \< 20 ) do j = j + 1 print( j ) end

In machine code, this breaks down to three machine instructions for the loop plus the amount of code to print the value of J out. So you’re looking at 60 instructions (plus calling print 20 times).  The older iPhone 4 could execute 2,000,000 instructions in a second. Your loop is only a few thousand instructions at best. The loop completes very quickly, probably faster than the plugin can event get the socket open to Parse.com.

Hi,

As Rob mentioned, the network call is asynchronous. Have a look at the network.request Corona docs to understand what is happening behind the scenes.

This isn’t so much an issue with the plugin, but how you decide to architect your application code.

Cheers.