- Home /
Fetching data from server hangs the game.
Hi,
I am using app42 services and fetching list of users in the beginning of the game but that takes too much time and hangs while adding that data to a list using for loop. May be the approach is not up to the mark. Any help would be great. Code snippet is below:
public class GetAllUserCallBack : App42CallBack
{
public void OnSuccess(object response)
{
IList<User> user = (IList<User>) response;
// App42Log.Console("userName is " + user.Count);
List <String> userNames=new List<string>();
for(int i = 0; i < user.Count; i++)
{
userNames.Add(user[i].GetUserName() ) ;
}
}
public void OnException(Exception e)
{
App42Log.Console("Exception : " + e);
}
}
Answer by HarshadK · May 09, 2017 at 11:31 AM
One way to counter the delay is:
If you are not displaying or processing all users at once then you can fetch the limited number of users first using the paginated users list to have a few users available to display and then fetch the remaining users later on (preferably, in a background thread so that it would not hamper the gameplay). You can fetch this remaining list of users as paginated list itself to avoid long delays in fetching. This is something similar to how websites load items from list using AJAX when user reaches near the end of the list.
i need to fetch all users at once as I need to compare with some other list
Something you need to learn to get around, when it comes to networking YOU CANNOT WAIT ON DATA. Seriously, it is a crippling thing to do. Find a way around it. Separate threads and events are helpful, create some sort of GUI element that pops up and says "Connecting Users..." so that your players are okay with waiting a moment.
Also, a very good practice to create a time-out limit for users who's data is not being collected in a timely manner. This will be annoying to users very rarely, and almost all games do it to prevent things like a bad connection ruining the game for all users.