question

Gordon Graber avatar image
Gordon Graber asked

GetUserData empty value returns what, exactly

Hi, I have a user data value for a key that is empty and I can verify that in the Player Data page of the PlayFab game title - that is what I expect and fine, but when I try detect in Unity C# that what is being returned by GetUserData is empty there seems to be a problem in comparing what ever is returned with string.Empty.

I can debug.log the returned key's value from the UserDataRecord and it shows me it is empty - that is: I can Debug.Log( ">" + returnedData + "<" ) and the output is "><" Showing me that the value is indeed empty.

Yet when I try to check if( returnedData != string.Empty ) 'true' is returned by the comparison.

How can that be?

What is actually returned by GetUserData when a key value is truly empty?

Player Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rick Chen avatar image
Rick Chen answered

Getting a user data that has empty value using GetUserData in Unity will return Null. The string.Empty you used is not Null, it is a string with zero length, please refer to: https://docs.microsoft.com/en-us/dotnet/api/system.string.empty?view=net-5.0

Please use the String.IsNullOrEmpty() instead to check whether the result is null or empty.

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gordon Graber avatar image Gordon Graber commented ·

Hi Chen thanks for your reply, before I saw you reply I put that test into my code

if( ! string.IsNullOrEmpty( _playFabReturnedFieldData ) )

on the same empty field and it still passes through true - as though it is neither null nor empty.

Perhaps this will help you diagnose the problem: Because of the overhead of checking if a field exists on the server - which in my understanding requires me to first make request to the server for a single field - I have been setting up some fields in advance to actually storing real data in them.

In this specific case, I have passed string.Empty for the data to be stored for one of these fields when it is first created.

That way I can retrieve a whole group of user data fields in one request, and then individually test if any of them are empty.

So I have passed string.Empty, but when I retrieve the data, which you are saying should be null, it is not seen by C# as either empty or null.

I guess the problem must be subtle. Perhaps I should pass "" for the field data when I create the filed, instead of string.Empty? I don't think there is supposed to be any difference between "" and string.Empty...

Or how else can I resolve this?

0 Likes 0 ·
Gordon Graber avatar image Gordon Graber commented ·

you know, I am just going to take the easy way out and just right a string I know can never be generated by my code, like literally the string "null" and that will solve the whole problem - if inelegantly!

0 Likes 0 ·
Gordon Graber avatar image Gordon Graber commented ·

Hey, sorry for all the posts - I spotted a problem in my code that was contributing to the issue. You were initially correct that isNullOrEmpty was working.

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ Gordon Graber commented ·

Glad that you spotted the problem. I double checked the String.IsNullOrEmpty() method, it is indeed working.

0 Likes 0 ·
Gordon Graber avatar image
Gordon Graber answered

Hi Chen thanks for your reply, before I saw you reply I put a test into my code

if( ! string.IsNullOrEmpty( _playFabReturnedFieldData ) )

on the same empty field and it still passes through true - as though it is neither null nor empty.

Perhaps this will help you diagnose the problem: Because of the overhead of checking if a field exists on the server - which in my understanding requires me to first make request to the server for a single field - I have been setting up some fields in advance to actually storing real data in them.

In this specific case, I have passed string.Empty when the field for the data to be stored for one of these fields when it is first created.

That way I can retrieve a whole group of user data fields in one request, and then individually test if any of them are empty.

So I have passed string.Empty, but when I retrieve the data, which you are saying should be null, it is not seen by C# as either empty or null.

I guess the problem must be subtle. Perhaps I should pass "" for the field data when I create the filed, instead of string.Empty? I don't think there is supposed to be any difference between "" and string.Empty...

Or how else can I resolve this?




10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.