Page 1 of 1

Can you cache SmartFox results on iOS and Android devices?

Posted: 07 Mar 2014, 16:55
by Robin
Hi,
I need to implement a cache system on iOS and Android and I was wondering if there is a way to cache the answers from the SmartFox server so that if the next time the user has no connection I can use some of the cached results to display the game.

Thanks

Re: Can you cache SmartFox results on iOS and Android device

Posted: 07 Mar 2014, 19:26
by Lapo
Nope, you can't and shouldn't do that. Server responses are never the same, if you recycle them you're going to break the client API's state.

Re: Can you cache SmartFox results on iOS and Android device

Posted: 13 Mar 2014, 10:16
by Robin
I see...well in my case I have some of the results that I know for sure they are the same. I have something similar to a structure like Google Maps with tiles that I want to cache when there's no connection. Isn't it possible to convert the responses to some form of text? Else I know how to do it by extracting all the data I need and save it in Hashtables and store those into memory but I was wondering if there was a simpler way. Any idea?

Re: Can you cache SmartFox results on iOS and Android device

Posted: 13 Mar 2014, 11:58
by Lapo
I have something similar to a structure like Google Maps with tiles that I want to cache when there's no connection.
Well this makes more sense. So you are not caching the actual server packet, you are storing the data.
Is it custom data you are sending from Extensions?
Isn't it possible to convert the responses to some form of text?
This I don't understand. If you are sending these "tiles" to the client, presumably you know how to deserialize the data, right?

Re: Can you cache SmartFox results on iOS and Android device

Posted: 14 Mar 2014, 09:40
by Robin
well I was wondering if I can store the SFSObject directly rather than extracting the data and save everything into arrays and hashtables. I saw that SFSObject has a ToBinary method that returns an array of bytes. I tried to save that into memory and it worked, the only problem is that now when I retrieve one of those tiles form cache I have an array of doubles (because that's how they get stored in memory) and I don't know how to convert it back to an array of bytes and then call:

SFSObject myTile = SFSObject.NewFromBinaryData(new Sfs2X.Util.ByteArray(tileBytes));

but when the line is called I get the error:

SFSCodecError: Invalid SFSDataType. Expected: SFS_OBJECT, found: 0
Sfs2X.Protocol.Serialization.DefaultSFSDataSerializer.DecodeSFSObject (Sfs2X.Util.ByteArray buffer)
Sfs2X.Protocol.Serialization.DefaultSFSDataSerializer.Binary2Object (Sfs2X.Util.ByteArray data)

I don't know if the problem is the way I convert the doubles back to bytes...I'm currently using:

static byte[] GetBytesAlt(double[] values)
{
byte[] result = new byte[values.Length *sizeof(double)];
Buffer.BlockCopy(values, 0, result, 0, result.Length);
return result;
}


Or should I not use the ToBinary method at all?


Thanks

Re: Can you cache SmartFox results on iOS and Android device

Posted: 14 Mar 2014, 10:19
by Lapo
well I was wondering if I can store the SFSObject directly rather than extracting the data and save everything into arrays and hashtables. I saw that SFSObject has a ToBinary method that returns an array of bytes. I tried to save that into memory and it worked, the only problem is that now when I retrieve one of those tiles form cache I have an array of doubles (because that's how they get stored in memory) and I don't know how to convert it back to an array of bytes and then call:
I am not following.
You have an SFSObject and you turn it into a Byte Array and store it in a HashTable.
Then you say that when you retrieve it, it is an array of doubles??

If you stored the Byte Array you will get a Byte Array, which is the only thing you can use to obtain the SFSObject back.

And there is one more question. Why don't you keep the SFSObject in memory instead of re-serializing and deserializing it a second time?

Re: Can you cache SmartFox results on iOS and Android device

Posted: 14 Mar 2014, 11:12
by Robin
Hi Lapo,
actually keeping the SFSObject in memory is what I would like to do. How do you do that efficiently so that it's easy to retrieve back? I'm trying now to store the binary data of the SFSObject into a file as byte[].

Thanks

Re: Can you cache SmartFox results on iOS and Android device

Posted: 14 Mar 2014, 12:39
by Robin
ok, I got it to work by saving the whole object byte[] array into a normal file and loading it with the File class ;)