Jump to content
microtronx

Android Service using local sqlite DB

Recommended Posts

As in different old topics written, it is still not possible to have a android service with a tfdconnection or a tsqlconnection on it. The service is not starting any more, you use this. 

 

Here's a link: https://forums.embarcadero.com/thread.jspa?threadID=229761

 

But what can we do? Is there a known solution? I'm not able to find one. Have tried to create all components in service's code but still, i don't even get a log entry that means the service still can't be created and the app hangs on trying to start the service.

 

Anone out there who can give some tips?

Share this post


Link to post

Have you tried to delete all Tfdconnection and Tsqlconnection? Still does not start?

Do you open Tfdconnection at runtime after the service start-up? Or it's set active at design time?

 

Toooooo many questions....

Share this post


Link to post
2 hours ago, Cristian Peța said:

Have you tried to delete all Tfdconnection and Tsqlconnection? Still does not start?

Do you open Tfdconnection at runtime after the service start-up? Or it's set active at design time?

 

Toooooo many questions....

Hi Cristian,

 

I configure and connect the connection at runtime after the service starts up. It is not connected at designtime.

 

1. if i remove the unit from implementation uses where tDatamodule with tsqlconnection / tfdconnection is, than the app and service starts.

2. If i let the unit in implementation uses and delete the tfdconnection/tsqlconnection from datamodule but let the interface-uses:   Data.DbxSqlite, Data.FMTBcd, Data.DB, Data.SqlExpr; than it hangs!
3. if i do the 2. but remote the units "  Data.DbxSqlite, Data.FMTBcd, Data.DB, Data.SqlExpr;" from interface-uses than it hangs

4. if i create the tsqlconnection and a tsqltable directly within the tServiceModule at runtime and add the needed units (see 2.) to interface-uses than it hangs

 

5. if i change the project file and remove the units for tDatamodule than i can create components at runtime within tServiceModule and it works !!

 

Seems that the problem is connected with tDatamodule? Dont know really... crazy,

 

Edited by microtronx
added 5.

Share this post


Link to post

I was able to track down things, it seems if we use Data.DB in tServiceModule or another unit which is used by the android-service the service is not able to start.

Now i create a tFdConnection directly in the tServiceModule by using only these units:

 

FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Comp.Client,

and now the Android Service is running fine. Next I will check if same happens, if we remote Data.DB from ClientModuleUnit's from Rest-Client ...

 

Edit: those FireDAC units are not enough, adding others >> service hangs .. i think some of them uses Data.DB ...

 

Ohh Embarcadero...

Edited by microtronx

Share this post


Link to post
17 hours ago, Alain Weber said:

Perhaps if you change the extention '.GDB' (or anything) of your database file to '.txt' it will run

I have removed all occurences of Data.DB Unit from my project and units and now the Service is working without problems. Seems that Delphi Rio latest Version has problems there ...

Share this post


Link to post

Is it a rights issue?  Can the app lack access to the location where the db is supposed to be?

Share this post


Link to post

Could the "features files" have something to do with it ?

https://en.delphipraxis.net/topic/1351-fmx-macos-missing-image-sqlite3-when-running-on-macos-sierra/?do=findComment&comment=11335

 

On 7/27/2019 at 1:04 AM, Dave Nottage said:

libcgsqlite3.dylib is now a "featured" file. Go to Deployment Manager and use the Add Featured Files to add it.

The answer was for Macos setting, but as fas as I understand will the featured files adding necessary librarties to all platforms when needed.

The hint that Sqlite "featured files" option is needed is still not present at Docwiki, but you can fint the option in the deployment manager.

Edited by Rollo62

Share this post


Link to post
On 1/23/2020 at 10:41 PM, microtronx said:

it seems if we use Data.DB

Mine works OK with that unit in it.

Share this post


Link to post
8 hours ago, Dave Nottage said:

Mine works OK with that unit in it.

Good morning Dave,

 

it is crazy but possible it has to do with Android 9? In my case everything works if i don't use the Data.DB and that means, i have to create all DB-Components in code. I'm doing this, using tSQLConnection and tSQLQuery for all access to SQLite Database files from app and from service.

 

It works but something is not ok in Delphi. I use latest Rio with all available patches.

Share this post


Link to post
13 hours ago, Rollo62 said:

Could the "features files" have something to do with it ?

https://en.delphipraxis.net/topic/1351-fmx-macos-missing-image-sqlite3-when-running-on-macos-sierra/?do=findComment&comment=11335

 

The answer was for Macos setting, but as fas as I understand will the featured files adding necessary librarties to all platforms when needed.

The hint that Sqlite "featured files" option is needed is still not present at Docwiki, but you can fint the option in the deployment manager.

I don't think it has to do with that, because I can use the sqlite db with creating components in code and excluding Data.DB from uses.

Share this post


Link to post
17 hours ago, Lars Fosdal said:

Is it a rights issue?  Can the app lack access to the location where the db is supposed to be?

Hi Lars, No rights are there. I'm requesting all rights on application start so we have access to filesystem ... as said, it works when i create all db-components in code and i can write / read.

Share this post


Link to post

Is there any design time settings on any of the db components that may be in violation at time of startup?

Share this post


Link to post
50 minutes ago, Lars Fosdal said:

Is there any design time settings on any of the db components that may be in violation at time of startup?

I have changed the code so the services does nothing, only adding the data.db in uses stops it from starting, removing the data.db everything ok again.

 

Share this post


Link to post
5 hours ago, microtronx said:

it is crazy but possible it has to do with Android 9?

I doubt it, since my device is Android 10. I'll have a play around tomorrow to see what I can deduce.

Share this post


Link to post

Hi All, thanks for trying to help me out.

 

Now after i was able to debug the android-service by creating a lot of toslog.d entries and viewing them in realtime with help of the DeviceLens Tool we see that the problem is not only the Data.DB!

 

If we use Data.DB (i had defined $ifdef's for this) then we have used also a function as described in 

 

And there is also the problem. Each time the service calls that function with a + in a string it gets appended to a server-method-call within the url and the service stops working because of an exception http/404!

I was not able to see this because the service is not debuggable from Delphi (afaik). Now after doing a check of strings / url-encoding them it works but this seems to be a bug in latest Rio Restserver / restclient impementations ... But using Data.DB also forces the service to hang, it can't even be started!

 

Edited by microtronx
not only Data.DB,
  • Like 1

Share this post


Link to post
10 hours ago, Dave Nottage said:

I've just checked in changes to this demo that include posting the location updates to a SQLite database. Service starts OK in it, at least for me.

 

Dave, you're the "Killer". Thanks a lot for this change in your demo. If i compare your project with mine the difference is:

 

yours:

        FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
        FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, FireDAC.ConsoleUI.Wait,
        FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,

 

to  mine:

        FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
        FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, FireDAC.FMXUI.Wait,

        FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,

 

Using your used units solved all hangers + i have removed the datamodule-unit and restclient-unit also from project files.

Everything is now working for some minutes without problems, very coool

 

Thank you very much, 
        

  • Like 2
  • Thanks 1

Share this post


Link to post

Thanks for sharing the root cause! Such mysteries can be so hard to track down!

I've seen this before on Windows - but didn't make the connection this time, if you pardon the pun.

 

Does an Android service have a Console define?

Our FireDAC helper unit uses that to handle Windows services and other console apps.

unit PSD_db_FireDAC;
/// <summary> Generic FireDAC wrapper.  Use PSD_db_FireDAC_MSSQL and PSD_db_FireDAC_PostgreSQL units </summary>

interface
uses
  System.Classes, System.SysUtils, Winapi.Windows,  System.Variants, System.AnsiStrings, System.TimeSpan, System.Diagnostics,
  System.TypInfo, System.Math,
  StringFunctions, TineInterfaces, TineClasses, PSDResourcePool, Generics.Defaults, Generics.Collections,
  PSD_Db_Abstract, PSDConstants, tiConsts, PSDLogEvent, PSDLogDrivers, PSDConnectivityStatus,
  Data.DB, FireDAC.Stan.Intf, PSDStatisticsEngine,
{$ifdef Console}
  FireDAC.ConsoleUI.Wait,
{$else}
  FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
{$endif}
  FireDAC.UI,
  FireDAC.UI.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.Phys.Intf,
  FireDAC.Stan.Def, FireDAC.Stan.Pool,  FireDAC.Stan.Async, FireDAC.Phys,
  FireDAC.Stan.Param, FireDAC.DApt, FireDAC.comp.DataSet, FireDAC.comp.Client,
  FireDAC.comp.Script,
  FireDAC.Moni.Base, FireDAC.Moni.RemoteClient, FireDAC.Moni.FlatFile;

 

Share this post


Link to post
6 hours ago, Lars Fosdal said:

Does an Android service have a Console define?

Our FireDAC helper unit uses that to handle Windows services and other console apps.

 

In my service i don't have that Console define because normally in a FMX Application we need FireDAC.FMXUI.wait ... but in case of android-service we need the FireDAC.ConsoleUI.wait

Share this post


Link to post
On 1/28/2020 at 9:19 PM, Dave Nottage said:

I've just checked in changes to this demo that include posting the location updates to a SQLite database. Service starts OK in it, at least for me.

Hi Dave, I use your demo for background-location-updates in a new app and there is one question. will that tLocation use a lot of energy/battery or will it only be active each x minutes as defined in this line:

 

FLocation.TimerTask.Schedule( (cLocationTimerInterval*60*1000) );

I also need a new timer in the service, which i create with tTimerTask.create and set the .schedule(..). Any tips for me for a better battery usage on the device?

Share this post


Link to post
15 hours ago, microtronx said:

will that tLocation use a lot of energy/battery or will it only be active each x minutes as defined in this line:

That's just the timer. Battery life will be affected by the settings used with the actual location services, as determined by MonitoringDistance and MonitoringInterval. There's some information here about the best strategies to use:

 

https://developer.android.com/guide/topics/location/strategies

 

 

  • Thanks 1

Share this post


Link to post
On 1/29/2020 at 7:42 AM, microtronx said:

 

Dave, you're the "Killer". Thanks a lot for this change in your demo. If i compare your project with mine the difference is:

 

yours:

        FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
        FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, FireDAC.ConsoleUI.Wait,
        FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,

 

to  mine:

        FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
        FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, FireDAC.FMXUI.Wait,

        FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,

 

Using your used units solved all hangers + i have removed the datamodule-unit and restclient-unit also from project files.

Everything is now working for some minutes without problems, very coool

 

Thank you very much, 
        

 

Again one note for others having also problems with Rio + Android + Service not starting:

 

If you use the unit FMX.Types in your service-units, it will crash also.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×