Category Archives: Advanced

Articles covering advanced topic areas

Creating PDF Reports in RAD Server

Creating PDF Reports in RAD Server

Reporting is a critical aspect of any enterprise application, but the data and the programming components to create reports are not always available on all platforms?

It took a little trial and error to work out the best path to create reports via RADServer using the components out the box.  In this post, I will share what I have learned along the way.

Reporting Components

The obvious answer to generating a report that can show on mobile is to create a report PDF report remotely download the PDF to the client over REST. But which version and platforms can be used?

Fast Reports (Embarcadero Edition), which is included in RAD Studio, provides everything we need to create a report and save it out to PDF for export.

Fast Reports – VCL or FMX?

Continue reading Creating PDF Reports in RAD Server

LiveBindings in VCL – Part 6 – Master Object, Detail TDataSet

Useful background to this post is available in post 4 (Linking to an Object for master detail) and post 5 (advanced master detail).

Filtering a TDataSet as detail of an object

Having explored how to link to a Master Detail relationship and created a funky anonymous method to return the data, I thought there is no reason why I should have to be returning linked data. The data could be from anywhere.

One example people have asked about when I’ve been at developer events is how to link an object to be the master for filtering a TDataSet… well, this actually provides a simple example for doing exactly this (as you can see in this video)

Continue reading LiveBindings in VCL – Part 6 – Master Object, Detail TDataSet

LiveBindings in VCL – Part 5 – Advanced Master Detail Objects

Please read LiveBindings in VCL – Part 4 – Master Detail before reading this blog / watching the video

In the last blog we looked at how to use master detail and how the BeforeScroll and AfterScoll events worked. I also created using Generics a Sync Object that allowed a single detail object to be linked to.

As promised, this blog post takes it a step further, using the previous foundation I have updated the code (so make sure you download the latest version from code central from the link below) to allow multiple objects of different types to be linked. Additionally, detail lists are also now available.

Link to code sample below…
Continue reading LiveBindings in VCL – Part 5 – Advanced Master Detail Objects

LiveBindings in VCL – Part 4 – Master Detail and Objects as Properties

This is the 4th video and blog in a series on using Visual LiveBindings mixed into existing VCL applications. 

In my last post, we looked at how to bind directly in code, using some common Visual LiveBinding Classes and in the next post we will look at more classes that can be used and the difference in the bindings they create. However before we get there a number of you asked about binding to master detail and sub objects. So lets explore this matter.

More in the blog post.

Continue reading LiveBindings in VCL – Part 4 – Master Detail and Objects as Properties

Delphi, C++ and InterBase Community

I’m sure some of you have seen the new Delphi, C++ and InterBase community platform that is being developed at Embarcadero. If not I would highly recommend a visit.

The Embarcadero Community is the new home to blogs, event diaries, latest news and features articles. While the old locations are still working, over time expect to see more and more on the community. Community is also social media ready with links for Facebook, Twitter, linkedIn and Google+

Community Welcome Page

Continue reading Delphi, C++ and InterBase Community

Free FireDAC training online

FireDAC Training

Free FireDAC training online!

FireDAC is the Universal Data Access library for developing applications for multiple devices, connected to enterprise databases.

Starting tomorrow you can join the embarcadero team for free FireDAC training online in these special sessions, where you will be taken from a beginner to an expert by showing you how to utilise FireDAC in multiple situations.

Having used multiple database technologies over the years of developing with Delphi, I have been exceptionally impressed by the raw power of FireDAC and how fast and easy it makes the most difficult jobs as a database developer.

The sessions run every Thursday from Jan 22-Mar 26 in 3 time zones so no excuse for missing out!

Register now for free

Date Topic
January 22 Introduction to FireDAC
January 29 Monitoring and Tracing
February 5 Cached Updates and Auto-inc Fields
February 12 Array DML
February 19 Cascading Options System
February 26 Preprocessing
March 5 ETL
March 12 In-Memory Dataset
March 19 Local SQL
March 26 BDE to InterBase Migration


Register now for free

Free XE7 Trial

If your not a XE7 customer you can get the most out of these session by downloading a Delphi XE7 free trial here.

Parallel Programming Thread Pool

In my Parallel Programming introduction post I explored how to easily get performance gains when running loop code by using the TParallel.For() loop construct. A key part of the Parallel Programming Library engine is the new ThreadPool that manages some of the complexity behind the scenes when using this syntax, but more on that later.

Following on from this first post I want to explore a common question I have heard. Is it possible to manage the Parallel Programming library thread pool Size? and if so how?

In short Yes, but I want to pose another question: Should you? – Lets explore this below.

Parallel programming thread pool

The Parallel programming thread pool is very smart! It automatically grows and shrinks based on CPU demand when your application runs and requires its use; it also throttles growth as your CPU usage rises ensuring it doesn’t over cook your CPU and ensuring you don’t lock up your machine. This inbuilt intelligence makes it very efficient and courteous out the box. All of this, without ANY management from us developers! 🙂 So why would you want to change this?

Thats not to say you can’t use a custom(ised) pool. If you do want to limit the size of a pool then you can override the defaults of  a TThreadPool.

TThreadPool Defaults

Defined in System.Threading, TThreadPool initiates with a default of 25 threads per CPU.

MaxThreadsPerCPU = 25;

TThreadPool multiplies the MaxThreadsPerCPU with the number of CPU’s on the machine (it gets this form calling TThread.ProcessorCount) and exposes the result via a read only property TThreadPool.MaxWorkerThreads.

To query the default pool size on your machine at runtime you can use TThreadPool’s handy class method that returns the default pool. With this pool you can then query the MaxWorkerThreads. e.g.

var
  i : integer;
begin
  i := TThreadPool.Default.MaxWorkerThreads;
  ShowMessage('Pool size = '+i.ToString());
end;

On my Windows VM running 2 cores I see 50, but on my Mac OS X with 8 cores, this code returns 200.

Customising a TThreadPool

Let me start this section by saying, modifying the default TThreadPool properties is not recommended. 

While possible, it is not recommended to modify the Default TThreadPool options as this is a global instance that is used throughout the application, and you never can be sure where and when its being used. You can however create your own instance of a TThreadPool that you modify and use and this is a better approach.

Creating and modifiying a TThreadPool

Creating a TThreadPool is as simple as declaring the variable and calling the constructor.

With an instance of a TThreadPool, you can then modify the MaxWorkerThreads by overriding the value using the method SetMaxWorkerThreads() which takes in an Integer. This sets it at a flat number regardless of the number of CPU’s you have available.  e.g. the following code reports 10 as the Max size on both my Windows and Mac OSX machines mentioned above.

var
  FPool : TThreadPool;

...

if Pool = nil then begin
  Pool := TThreadPool.Create;
  Pool.SetMaxWorkerThreads(10);
end;

Note, the MaxWorkerThreads number must always be greater than the MinimumWorkerThreads value that (by default) is set from TThread.ProcessorCount.

A word of caution..

Creating a new TThreadPool come with an overhead. From an initial test where I creating a new TThreadPool for running a small TParallel.For() loop, and then disposing it afterwards it actually decrease performance on your application compared to a traditional for loop. For this reason alone, I would always use a global TThreadPool. When the pool was created globally, the speed performance was immediately backup compared to the create and destroy on demand idea.

 Example of using a custom TThreadPool

Below is an example where Pool is a global TThreadPool.  When the button is selected to run a TThreadPool with a maximum of 10 WorkerThreads. The only adjustments from the example in the previous tutorials is that we now pass in Pool as a paramater to the For loop, note however that this is not being created and free’ed each time in this code.

var
  Pool: TThreadPool;

procedure TForm5.Button1Click(Sender: TObject);
var
 Tot: Integer;
 SW: TStopwatch;
begin
 // counts the prime numbers below a given value
 Tot := 0;
 SW := TStopWatch.Create;
 SW.Start;

 if Pool = nil then begin 
   Pool := TThreadPool.Create;
   Pool.SetMaxWorkerThreads(10);
 end;
 TParallel.For(1, Max, procedure (I: Integer)
   begin
     if IsPrime (I) then
       TInterlocked.Increment (Tot);
   end,Pool);
 SW.Stop;
 Memo1.Lines.Add (Format (
 'Parallel (Custom Pool) for loop: %d - %d', [SW.ElapsedMilliseconds, Tot]));
end;

I would say that while this gives me a sense of control, I actually don’t like the fact that I am messing with something that is highly tuned. I would personally conclude that a ThreadPool should be created as the application has initialised and that you use this. Ideally I would say use the default one, as it behaviour is very good already, but if you really want to make more work for yourself, then you can always set the properties of a new pool and use it.

 A thank you to Allen Bauer for his input while writing this post.

NFC in Android from Delphi / Objective Pascal

One great thing about developing with Embarcadero’s RAD Studio, Delphi, C++ Builder and AppMethod is the component development model. Developing with components allows you to write code once that works across all platforms as the components take care of the platform specific API mappings. e.g. Talking to the camera, accelerometer, compass etc.

There are however times when a component doesn’t exist (yet). This is normally when something is platform specific as it doesn’t make sense to have a component that is platform specific when you can still call the API’s of that platform. e.g. prior to the announcements from Apple on the iPhone 6, NFC was on Android but not iOS. 

So what do you need to do when you don’t have a component ready to go? – Well it depends on what you want to call. With regards to accessing NFC, then Brian Long and Danny Magin, both Embarcadero MVP’s, have recently blogged about working through importing the Java libraries required, setting up the intents to collect the message from NFC, setting your application so Android knows it can deal with messages from NFC and then using them. Pretty cool 🙂

If your interested further in calling platform API’s then check out the replays from the Skills Sprints for accessing Android and iOS API’s directly.