简体   繁体   中英

RoboSpice and ORMLite - Access to the data

I just started using RoboSpice for a new Android application. RoboSpice is used with ORMLite and SpringAndroidSpiceService to read JSON from a REST Web Service.

For now, I managed to:

  • create a custom SpiceService
  • make few request to get data from the WS
  • persist data to DB with ORMLite

... thanks to the "robospice-sample-ormlite" sample .

Main parts of my app (questions in bottom):

BaseActivity

public abstract class BaseActivity extends RoboActivity {

    private SpiceManager spiceManager = new SpiceManager(MySpiceService.class);

    @Override
    protected void onStart() {
        super.onStart();
        spiceManager.start(this);
    }

    @Override
    protected void onStop() {
        if (spiceManager.isStarted()) {
            spiceManager.shouldStop();
        }
        super.onStop();
    }

    protected SpiceManager getSpiceManager() {
        return spiceManager;
    }

}

SplashActivity

public class SplashActivity extends BaseActivity {
    ...
    @Override
    protected void onStart() {
        super.onStart();
        ...
        getSpiceManager().execute(foosRequest, new Integer(0), DurationInMillis.ONE_HOUR, new FoosRequestListener());
        getSpiceManager().execute(barsRequest, new Integer(1), DurationInMillis.ONE_HOUR, new BarsRequestListener());

        // Start MainActivity after X seconds (handler + startActivity(intent...)
        ...
    }

    public final class FoosRequestListener implements RequestListener<Foos> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Toast.makeText(MainActivity.this, "failure foo", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRequestSuccess(final Days result) {
            Toast.makeText(MainActivity.this, "success foo", Toast.LENGTH_SHORT).show();
            String originalText = getString(R.string.textview_text);
            mLoremTextView.setText(originalText + result.getResult().iterator().next().getMoonrise());
        }
    }

    public final class BarsRequestListener implements RequestListener<Bars> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Toast.makeText(MainActivity.this, "failure bars", Toast.LENGTH_SHOR ).show();
        }

        @Override
        public void onRequestSuccess(final Months result) {
            Toast.makeText(MainActivity.this, "success bars", Toast.LENGTH_SHORT).show();
        }
    }
}

MySpiceService

public class MySpiceService extends SpringAndroidSpiceService {
    ...
    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        List<Class<?>> classCollection = new ArrayList< Class< ? >>();

        // Add persisted classe(s) to class collection
        classCollection.add(Foos.class); // Collection of "Foo"
        classCollection.add(Foo.class);
        classCollection.add(Bars.class); // Collection of "Bars"
        classCollection.add(Bar.class);

        // Init
        RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper(application, DATABASE_NAME, DATABASE_VERSION);
        InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory(application, databaseHelper, classCollection);
        cacheManager.addPersister(inDatabaseObjectPersisterFactory);

        return cacheManager;
    }

    @Override
    public RestTemplate createRestTemplate() {
        ...
        return restTemplate;
    }
}

The application is composed of a splash screen (SplashActivity) and other activities. The splash screen start the MainActivity after few seconds (just a timer for the moment).

What I would like to do is download all the data from the splash screen (RoboSpice already cached/persist these data in the SQLite DB) and then access it from other activities.


Question 1: Is it possible to access and manipulate the database data outside of the SplashActivity? How (can you provide some code examples)?

The Wiki page about "RoboSpiceDataBaseHelper" is not very clear for me: it is stated that "RoboSpiceDataBaseHelper is used to manage database creation and version management" but in the next paragraph it is stated that some method allow "querying and retrieving data from the database". In the RoboSpice samples, there is nothing about retrieving data from other activities. How to leverage the power of ORMLite?

I have read from this Google Groups topic from Snicolas:

Note that RoboSpice caching is meant to be transparent and is completely encapsulated in the SpiceService. It should be possilbe to access the cache system (in your case the databse) directly through a shared reference to it (shared by the service and your activities). Also, in that case, nothing will prevent side-effects like concurrent access or database inconsistency.

So, how? I'm totaly lost :(

Edit 03/01: Due to my lack of competence with RoboSpice and ORMLite, I'm confused how to implement such a solution / to do this myself (at least efficiently). I don't see how to use / combine RoboSpiceDatabaseHelper, InDatabaseObjectPersister, InDatabaseObjectPersisterFactory...


Question 2:

My last question is not related to data access (I take this topic to ask).

Why getSpiceManager().execute(barsRequest, new Integer(1), DurationInMillis.ONE_HOUR, null) do nothing (without callback to the RequestListener) ? Infact, in my case (splash screen), I don't need of the callback. Can I avoid it?


Thanks!

Question 1 :

you have to share the RoboSpiceDatabaseHelper between your service and the places you want to access it.

The cleanest way is to use an injection framework like RoboGuice. A quick and dirty hack is to create a static provider for this instance, or even dirtier and quicker, make the RoboSpiceDatabaseHelper a singleton.

---UPDATE

As you use RoboGuice, you can :

Create your own RoboSpiceDatabaseHelper class an annotate it with @Singleton :

@Singleton
public class MyRoboSpiceDatabaseHelper extends RoboSpiceDatabaseHelper {
...
}

In both your spiceservice and in the classes that need an access to the database, you can declare a member like :

@Inject
RoboSpiceDatabaseHelper databaseHelper;

You will get a proper singleton then, allowing you to share data base access.

Question 2 :

if you really need to go fast, simply create a stub/dummy listener. RS won't execute a request without listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM