使用我的测试Android应用程序,我试图计算如果我的用户每10秒或1分钟添加一个孩子并与5个设备同步将需要多少带宽。
我的应用用户每分钟都会创建数据,所以我必须计算每个用户每月的订阅费用。每个用户每月消耗的近似带宽。
我在MainActivity中调用addChildEventListener,每隔10秒从BroadcastReceiver类添加一个子代。
每次我重新启动应用程序时,它都会下载所有行,这意味着消耗带宽。
如何在每次重新启动应用时减少带宽使用量并让应用只检索新添加的孩子?
如果我添加Firebase.getDefaultConfig()。setPersistenceEnabled(true); 用户在线还是addChildEventListener下载所有数据,从服务器或本地同步数据下载?
这就是我想要的。
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button start, stop;
TextView status;
String url = "https://<my-app>.firebaseio.com/data";
Firebase mFirebaseRef;
AlarmManager alarmManager;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
mFirebaseRef.getDefaultConfig().setPersistenceEnabled(true);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
start = (Button) findViewById(R.id.Start);
stop = (Button) findViewById(R.id.stop);
status = (TextView) findViewById(R.id.serviceText);
mFirebaseRef = new Firebase(url);
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("Data onChildAdded", dataSnapshot.getValue().toString());
//Toast.makeText(getBaseContext(), "data=" + dataSnapshot.getValue(), Toast.LENGTH_LONG).show();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.d("Data onChildChanged", dataSnapshot.getValue().toString());
//Toast.makeText(getBaseContext(), "data=" + dataSnapshot.getValue(), Toast.LENGTH_LONG).show();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d("Data onChildRemoved", dataSnapshot.getValue().toString());
//Toast.makeText(getBaseContext(), "data=" + dataSnapshot.getValue(), Toast.LENGTH_LONG).show();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.d("Data onChildMoved", dataSnapshot.getValue().toString());
//Toast.makeText(getBaseContext(), "data=" + dataSnapshot.getValue(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status.setText("Started..");
Intent myIntent = new Intent(getApplicationContext(), MyServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),10000,
pendingIntent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status.setText("Stopped");
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), MyServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager.cancel(pendingIntent);
}
});
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
BroadcastReceiver.java
public class MyServiceReceiver extends BroadcastReceiver {
Firebase mFirebaseRef;
Firebase mFirebaseRef2;
String url = "https://<my-app>.firebaseio.com/data";
Context context=null;
String data = "With Firebase queries, we can selectively retrieve data based on various factors. To construct a query, you start by specifying how you want your data to be ordered using one of the ordering functions: orderByChild(), orderByKey(), orderByValue(), or orderByPriority()";
@Override
public void onReceive(final Context context, Intent intent) {
this.context = context;
Firebase.setAndroidContext(context);
mFirebaseRef = new Firebase(url);
Firebase ref = mFirebaseRef.child(System.currentTimeMillis()+"");
ref.setValue(new RecordingModel("Meeting","https://www.stadd.com",data));
Log.d("caught", "data saved");
}
}
答案 0 :(得分:4)
您可以使用Firebase Queries来限制侦听器下载的数据。因此,您不必将ChildEventListener
直接附加到Firebase位置,而是存储您在某处收到的最后一个孩子的密钥(例如,SharedPreferences),然后在下次加载时从该子项开始加载。
这是监控最后一项密钥的快捷方法:
String lastChildKey;
mFirebaseRef.orderByKey().limitToLast(1).addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot snapshot, String s) {
lastChildKey = snapshot.getKey();
}
})
有了这些知识,您可以从应用程序重新启动时上次看到的项目开始:
Query newItems = mFirebaseRef.orderByKey().startAt(lastChildKey);
newItems.addChildEventListener(...
使用这种方法,您必须管理自己的客户端状态,因为您实际上是用自己的方式替换Firebase的客户端缓存/同步策略。