Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Parse Plugin and Execution Order
Started by awvieweg Dec 18 2015 04:08 PM

2 replies to this topic
parse topdown login
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

awvieweg

[GLOBAL: userInfoPane.html]
awvieweg
  • Observer

  • 10 posts
  • Corona SDK

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!



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,781 posts
  • Enterprise

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.



[TOPIC: post.html]
#3

Develephant

[GLOBAL: userInfoPane.html]
Develephant
  • Corona Geek

  • 1,450 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]