How to wait function OnSucess or OnException finish to continue ?

0 votes

Hi, i want to know how to wait OnSucess or OnException finish to continue and do an getResult.

Can someone help me please ? Its a basic question but im stuck here.

 

void TaskOnClick()
    {
        userService.Authenticate (username.text, password.text, callback);
        callback.GetResult(); // return the string of OnSucess or OnException
    }
 
Thanks :)
asked Jan 15, 2017 in Unity by waloucg (16 points)

5 Answers

+1 vote
 
Best answer
try this
public class UsersTest : MonoBehaviour {
 
	static UsersTest classInstance = null;
    static string result = "";
    public Button createUserButton;
    public InputField username;
    public InputField password;
    public Text connectionStatus;
    UserService userService;     
 
    // Use this for initialization
    void Start () {
		classInstance = this;
        App42Log.SetDebug(true);       //Print output in your editor console  
        App42API.Initialize("API_KEY","API_SECRET_KEY");
        userService = App42API.BuildUserService();
        createUserButton.onClick.AddListener (TaskOnClick);
    }
         
    void TaskOnClick()
    {         
        userService.Authenticate (username.text, password.text, new LoginCallBack());
    }
	
	void receiveResult()
	{
		Debug.Log (result);
		//do something with result
		//...
	}
	
	public class LoginCallBack : App42CallBack
    {
        public void OnSuccess(object response)
        {
            User user = (User)response;
			result = "succes";
			classInstance.receiveResult();
			Debug.Log("userName is " + user.GetUserName()); 
			Debug.Log("sessionId is " + user.GetSessionId());
        }
        public void OnException(Exception e)
        {
			result = "exception";
			classInstance.receiveResult();
            Debug.Log("Exception : " + e);
        }
    }
}

 

answered Jan 19, 2017 by copangtnc (45 points)
selected Jan 19, 2017 by waloucg
I get an error CS0038: Cannot access a nonstatic member of outer type `UsersTest' via nested type `UsersTest.LoginCallBack'

EDIT : My bad i delete the static before UsersTest, thanks you verry much man you resolve my problem, now the function is called after the callback thanks you verry much !!
0 votes
Hi,

If I have understood your query correctly then you want to call this API synchronously. I mean you dont want to proceed until you get the response from the server. If Yes, then you can put a loader on the screen before calling this API and remove that loader after you get the response in onSuccess or onException callbacks. You can perform further task once either onSuccess or onException callback is fired.

Let me know if this is not the case.

Thanks.
answered Jan 16, 2017 by rajeev.etc (1,660 points)
Yeah, i just want call callback.GetResult when the function OnSucces or OnException is finish, the only way i find its to GetResult in the update with some if but i dont like this method, not verry optimized and secure.
0 votes
Anyone for help me, its seem a basic question for user who use app42 ?
answered Jan 18, 2017 by waloucg (16 points)
0 votes
Hi, Greetings from ShepHertz!!!! Yes, you don't need to write the code in Update function.To achieve this you can follow either of these solution. 1. Can implement App42CallBack in your Unity Class and override OnSuccess and OnException method in the same class so that you can get the result you are getting in the call. 2. You can create your own interface with the methods based on your use Cases, and implement the same method by overriding in your Unity Class. In Callback file you can pass the reference of your Unity class. Once you will get the response in OnSuccess and OnException, you will parse the same and able to call your own interface methods from here those you have overrided in your Unity class. Let me know if it helps. Thanks Vishnu Garg App42 Team
answered Jan 18, 2017 by Vishnu Garg (674 points)
0 votes

I post my code for explain


public class UsersTest : MonoBehaviour, App42CallBack {

	string result = "";
	public Button createUserButton;
	public InputField username;
	public InputField password;
	public Text connectionStatus;
	UserService userService;
	

	// Use this for initialization
	void Start () {
		App42Log.SetDebug(true);       //Print output in your editor console  
		App42API.Initialize("API_KEY","API_SECRET_KEY");
		userService = App42API.BuildUserService();
		createUserButton.onClick.AddListener (TaskOnClick);
	}
		
	// Update is called once per frame
	void Update () {

	}
		
	void TaskOnClick()
	{
		
		userService.Authenticate (username.text, password.text, new UsersTest());
                Debug.Log (result);
	}
        public void OnSuccess(object response)  
	{  
		User user = (User) response; 
		result = "succes";
		App42Log.Console("userName is " + user.GetUserName()); 
		App42Log.Console("sessionId is " + user.GetSessionId());
	}  
	public void OnException(Exception e)  
	{  
		result = "exception";
		App42Log.Console("Exception : " + e);
	}

So, i create a listener on my button for execute TaskOnClick when the button is pressed. You tell me to do the callback inside my class so its what i do here, the problem its when OnSucces or OnException work, he dont find my result string anymore, i need to recreate her. So if i want to modify a gameObject inside of the OnSucces or OnException i need to find it again and i dont like this way, its why i want to get the string like "succes" etc and do my stuff in my class. So if you can help my on this code it would be cool (sorry for my bad english).

Debug.Log is executed just after the Authenticate function and not the callback.

answered Jan 18, 2017 by waloucg (16 points)
Before Calling any API you can set the result String status as pending and can do necessary for that and once you will gets the solution the result String will change and you will be able to find the result.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...