Forum BigDB Callbacks not firing?

Discussion and help relating to the PlayerIO database solution, BigDB.

Callbacks not firing?

Postby Firebrand » March 15th, 2013, 7:23 am

I've got this bit of code, pretty simple (Unity);

RemovedMatchStatus = -2; //will set to -1 or Time.time using callbacks

string[] keys = new string[] {_currentMatch.Key}; //_currentMatch is a BigDB object
myPlayerIOClient.BigDB.DeleteKeys(
"PlayerObjects",
keys,
delegate() {
RemovedMatchStatus = Time.time;
},
delegate(PlayerIOError error) {
if(Debug.isDebugBuild) Debug.Log("Error deleting a Match: " + error.ToString());
RemovedMatchStatus = -1; //failed
}
);

Trouble is 'RemovedMatchStatus' seems to stick at -2 so I assume the Callbacks don't fire and I'm not sure why.
Any ideas why?
Firebrand
 
Posts: 12
Joined: February 20th, 2013, 11:58 pm

Re: Callbacks not firing?

Postby Henrik » March 15th, 2013, 1:06 pm

Callbacks are executed asynchronously, on another thread.

So in your main thread, you set RemovedMatchStatus to -2, and call DeleteKeys. On this thread, RemovedMatchStatus is never modified.

Some time after that, the callback fires, in a different thread. And in there, it will set RemovedMatchStatus to something other than -2, and then the callback is done. But your main thread is long gone, so your code there won't see the change.

If you need to take action after deleting your object, all of that code has to go inside the callback.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Callbacks not firing?

Postby Firebrand » March 15th, 2013, 4:53 pm

OK, I can move some code into the Callbacks, but 'RemovedMatchStatus' is a static variable, so it should still work.

(I was checking some code in my main loop and waiting for RemovedMatchstatus to change before running certain routines - eg another queued delete call as Matches were already in the middle of deleting (in theory) ) This obviously requires waiting 'x' frames, which isn't really something I can do in a callback easily.
Firebrand
 
Posts: 12
Joined: February 20th, 2013, 11:58 pm

Re: Callbacks not firing?

Postby Henrik » March 15th, 2013, 5:07 pm

Firebrand wrote:OK, I can move some code into the Callbacks, but 'RemovedMatchStatus' is a static variable, so it should still work.

We do not allow static variables, you should have gotten a warning about that in the Development Server?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Callbacks not firing?

Postby Firebrand » March 15th, 2013, 5:30 pm

Just checked. Don't see any static variable warnings.
The variable is in the same Script as the routine, does that make a difference?

So how, would I pesist data checking - write to a public variable and use separate code to keep variable alive if I swap to a scene without that variable?

EDIT: Spent 2 seconds thinking and will just create a persistent object (dontDestroyOnLOad..) that holds the relevant variables and use Object find and access.... that should solve the problem.
Firebrand
 
Posts: 12
Joined: February 20th, 2013, 11:58 pm

Re: Callbacks not firing?

Postby Henrik » March 16th, 2013, 11:35 am

Oh, right you're using the client library, not the server-side. Sorry, I got a bit confused. Disregard what I said about static variables then.

The client library also has synchronous, blocking, methods available:

http://playerio.com/documentation/refer ... DeleteKeys (the one without callbacks)

If you use those, your code will do the delete, and then wait until it's complete before continuing. That's a simpler code model to work with, but it might lead to an unresponsive game, so you have to make sure to never block on the UI thread or whatever similar concept Unity has.

In your case, if you always want to wait for the Delete, using the synchronous method is a good choice.

As for why your original code doesn't work, I would put some debug print statements when the callback fires and when you try to read the RemovedMatchStatus variable, and make sure that the timing is right, that you don't try to read it before it has been changed. And if the timing is correct, another source of error could be that the variable is ThreadStatic, which means that even though it is a static variable, there's one copy for each thread that accesses it, instead of a single global variable.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Callbacks not firing?

Postby Firebrand » March 16th, 2013, 11:58 pm

According to Debug data, the Callbacks weren't firing as I never saw the Debug messages.

I was trying to avoid deleting new stuff while a delete was occurring, to keep stuff synced. Instead I moved the timing of the delete of Objects from a 'game over' section to my main loop, having made a LIst of DatabaseObjects that needed deleting as they were finished with.

eg List<DatabaseObject>() += my finished game Object - so game would not call Delete routine there, but my main loop now reads this list and performs the Delete routine if the List has any data, before clearing the List.

This works perfectly, which is great. But I'm the type who would like to understand why it didn't work if same routine is called in Scene A, and variable is read in my mainloop (Scene B). I'm pretty sure I have the 1 static and no copies....at least this code re-arrangement works and always fires the Callbacks. I guess it is some kind of thread related issue, they can be hard to track sometimes.
Thanks for your input, appreciated.
Firebrand
 
Posts: 12
Joined: February 20th, 2013, 11:58 pm

Re: Callbacks not firing?

Postby dreamora » March 17th, 2013, 2:59 pm

I am not sure that this is potentially related to your cause but:

In the context of Player.IO Client and Unity one crucial warning has to be made that will cause a lot of strange behaviour, phantom crashes and access exceptions otherwise: Don't touch anything that is extended from UnityEngine.Object (including MonoBehaviour, Transform and all other components) in the callbacks or Unity will crash.
Unity is not threadsafe and does not support threaded usage of its wrapper classes (UnityEngine.object and its extended classes are wrappers around C / C++ data in the actual engine) and Player.IOs SDK out of the box is unluckily ignores this aspect and its consequences.

For proper integration into Unity, you must feed the callback results that are meant to access or be used in relation to any part of Unity (aside of Debug.Log and similar static classes) into a message queue that you process from a coroutine, Update or LateUpdate within a MonoBehaviour.
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: Callbacks not firing?

Postby Firebrand » March 17th, 2013, 8:25 pm

Thanks for that last reply. Very useful.
Given my code layout that sounds like the reason. Threads are often a little awkward with different systems integrating so I'll bear this in mind and rearrange future code accordingly.
Firebrand
 
Posts: 12
Joined: February 20th, 2013, 11:58 pm

Re: Callbacks not firing?

Postby gl33mer » September 11th, 2013, 1:55 pm

Yes. Thanks for that one.
gl33mer
 
Posts: 5
Joined: September 11th, 2013, 1:37 pm


Return to BigDB



cron