1

I have an app that does the next thing: receives GPS data through DDMS and stores them in a database and while the data are stored in the database I should also start a client thread that reads the new data stored in the database and sends it to a remote server!!!

In order to receive GPS data I do something like this:

 lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


  locationListener = new MyLocationListener();


   lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

And in my LocationChanged method I insert the GPS data in a database:

private class MyLocationListener implements LocationListener 

    {
        @Override

        public void onLocationChanged(Location loc) {

            if (loc != null) {

                latitude=(int)(loc.getLatitude()* 1E6);

                longitude=(int)(loc.getLongitude()* 1E6);

              db.insertData1(longitude,latitude);
           }

        }

And now my problem is:

How/where should I start the client thread....which reads the database?

It first tried to start the client thread right after this line:

m.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

But if I do that I have the risk to get a force close cause the Client thread reads the database which may be empty.

How should I know when to start reading the database?

Should I use the protocol wait/notify for the client thread so while I get GPS update I read the database???? Who should I implement wait/notify within one thread??Thx...I'm here for further questions:)

adrian
  • 4,574
  • 17
  • 68
  • 119

2 Answers2

1

wait/notify is for synchronizing access to shared data when multiple threads access it concurrently. It does not apply in your case.

What you need is to simply check if database exists before you start reading it: Query if Android database exists!

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Well that's not enough!If the database exists and has some GPS data in it(not all the data ) I start reading it and send the data at the server.....but I still can receives updates after that and this will never be send!!!! – adrian May 06 '11 at 18:34
1

It sounds like you are trying to use wat / notify as a general purpose way to pass messages. it's not.

If you truly want B to run only after A is finished, then do that. run A then B on your main thread. no need to mess w/ synchronization / wait / notify.

If A and B can run simultaneously, create a lock object that's shared between the DB thread and the send thread,

Object lock = new Object();

In each of the threads, synchronize your operations on this object.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Actually the reading in the DB is done within the client thread.Which is my second thread cause for doing all the location updates I don't fire another thread?And another thins is that I have to send the location updates as soon as I receive them....I can't wait until all the updates are received and then send them(I have my reasons) – adrian May 06 '11 at 18:32
  • that's fine, two threads, one lock object. just sync the DB.add() and the locations.send() methods (or whatever you call them) with that lock. i hope your app makes it clear that you are recording and sending the user's location off, and why you are doing so. – Jeffrey Blattman May 07 '11 at 01:30
  • and finally, as peter said, the location.send() operation needs to be written robustly such that if the DB is non-existent, or empty, the call is a no-op. that's much better than trying to ensure that A happens before B. – Jeffrey Blattman May 07 '11 at 01:31