开发者

Android architecture: Where should we hold the Cursor(s)?

开发者 https://www.devze.com 2023-02-02 06:49 出处:网络
Scenario: public class MyApplication extends Application{...} Three Activities, Overview, List and Map,

Scenario:

public class MyApplication extends Application{...}

Three Activities, Overview, List and Map,

displaying the same data only providing different UI/UX.

Data fed into Contentprovider db and frequently updated externally by a SyncAdapter,

part of another application.

Read data from Contentprovider in to cursor.

Do cursor.setNotificationUri(),

to make the cursor listen for db/Uri changes.

Alt1. Hold the cursor in MyApplication.

Overview, List and Map then asks MyApplication for data.

On a db change MyApplication holds a reference to each Activity

and notifies them to ask for the data again from MyApplicati开发者_Go百科on.

Alt2. Hold one cursor in each Activity.

On a db change each Activity requeries the cursor for the data again.

Where should we hold the Cursor(s) ?


I'm facing a similar problem but not with DB and cursor but simple web request\response.
The problem u'll start facing soon enough is what ahppens when your activity dies in the midst of a request response. I would suggest the following:

  1. make a base activity u'll inherit from that will contain a cursor for the query.
  2. make sure you have a method to fill the currsor with data in onResume start listening to broadcast receiver.
  3. when you finish a db update in the separate thread, notify all receivers.
  4. when you perform on create check the data in the DB to see if you missed a call from the broadcast because your activity has died.

so basically u will have a cursor in each activity, but no need to manage it by code unless you need to do something that is not standard with it. don't forget to dispose the cursor once the activity dies.

0

精彩评论

暂无评论...
验证码 换一张
取 消