views:

130

answers:

2

Hi there,

I'm making a shopping list app which basically uploads your shopping list to a php file and also downloads all the updates anyone else has made to the list.

I'm using record stores w/ record enumeration and an item object

Basically i want to be able to send off all the elements in the record store to the php file using a thread. The trouble comes from, do i need to pass the record store to the thread? and how do i get the data back and update the record store?

At the moment i cant see how my Send data class is going to update the record store of the main midlet.

Thanks in advance

edi

public GetDataClass(Midlet parentMidlet, String URL, RecordStore tempRecordStore)
{
    try
    {
        populateLocalRecordStore(tempRecordStore);
        this.parentMidlet = parentMidlet;
        this.URL = URL;
    }
    catch(Exception e)
    {
        System.out.println(e.toString());
    }
}

the populate record store takes the record store that has been passed and literally loops through all records and inserts them into the local rs. The problem comes from when i want to pass the data back to the main form/recordstore

edit

How do i update the record store in the main form from within the thread (what has been returned from the http request)

+1  A: 

If I understand you correctly (please let me know if I am not) you want to know how you can get a return value from the background thread that should update the UI? The thread you are using to populate the local record store.

So, if you GetDataClass is Runnable (created with new Thread(runnable)), you can do something similar to this:

public class GetDataClass implements Runnable {
    private Midlet parentMidlet;
    private String URL;
    private RecordStore tempRecordStore;

public GetDataClass(Midlet parentMidlet, String URL, RecordStore tempRecordStore) {
        this.parentMidlet = parentMidlet;
        this.URL = URL;
        this.tempRecordStore = tempRecordStore;
}

public void run() {
    try {
         returnData = populateLocalRecordStore(tempRecordStore);
         parentMidlet.updateForm(returnData);
    } catch(Exception e) {
          // log and do exception handling
    }
}
Jarle Hansen
+1  A: 

you can do what Jarle said, but that is not enough. Record stores cant handle concurrency by themselves, so if you have 2 threads accessing the same record store, you'll need to use some locks to control the access:

On your main thread:

private Object lock = new Object();
public Object getLock() {
    return lock;
}

public void retrieveDataFromRS() {
    synchronized (getLock()) {
        //read, edit, whatever your RS here and release the lock
    }
}

On your GetDataClass thread:

public void run() {
    synchronized(parentMidlet.getLock()) {
        //read, edit, whatever your RS here and release the lock
    }
}

you can synchronize them using the class or the own thread, but creating a lock object gives you more control, as you can stop one thread until theres more data, or whatever you need using the getLock().wait() to make the thread wait until someone notify the lock, and getLock.notify(), to make all threads waiting on the lock to start running again.

eMgz