AppTethering Leaderboard – Passing Objects Remotely and using Resources

AppTethering opens up a range of powerful features for coupled applications. Beyond the basic features for calling remote actions (which is rather cool and quick to setup) it is also possible to share resources between applications.

To give you an example I have built a Scoreboard that records the best scores in a simple game. As the scores come in and the leaderboard is updated, it rebuilds the resource that is then automatically sent out to the connected clients.

Using the TTetheringAppProfile.Resources you can add at Design time or run time a TLocalResource. The TLocalResource has a concept of being either Shared or Mirrored (set via the Kind property). In the example of the leaderboard both the Game (clients) and Leaderboard (server) have the same resource added to their TetheringAppProfile with the difference being that the clients have the kind as Mirror rather than Shared.  As the central Shared resource is updated this is broadcast to the clients and the TLocalResource then fires its OnResourceReceived event.

So with that in mind, see it in action and download the code sample.

How to convert an object to JSON and back with a single line of code

Ever wanted to take an Object into a format that is easily persisted and back? Well now you can. New in XE6 is the REST.JSON unit. This allows you access to TJSON a class with some very helpful class methods. Using TJSON you can convert an object to a JSON string and back with a little help from generics along the way.

The following code uses a class called TFoo that has a Foo and a Fee property (string and Integer) Using TJson you can then see how to covert the object to a string and back ready for storage, transport etc.

uses REST.JSON; // Also new System.JSON
procedure TForm1.Button1Click(Sender: TObject);
var
  Foo: TFoo;
begin
  Foo := TFoo.Create;
  try
    Foo.Foo := 'Hello World';
    Foo.Fee := 42;
    Memo1.Lines.Text := TJson.ObjectToJsonString(Foo);
  finally
    Foo.Free;
  end;
  Foo := TJson.JsonToObject<TFoo>(Memo1.Lines.Text);
  try
    Foo.Fee := 100;
    Memo1.Lines.Add(TJson.ObjectToJsonString(Foo));
  finally
    Foo.Free;
  end;
end;

If you want to watch this being demo’ed this very show video shows it in action http://youtu.be/TSqWoFvjj5g