Android 尽量减少从Firebase的下载
在应用程序的NotificationListener中,我正在侦听(并与SQLite同步)来自Firebase的新数据或更改的数据。 在第一种情况下,我想检查借书的状态是否发生了变化。因此,我在onStartCommand中使用以下代码:Android 尽量减少从Firebase的下载,android,firebase,firebase-realtime-database,android-notifications,Android,Firebase,Firebase Realtime Database,Android Notifications,在应用程序的NotificationListener中,我正在侦听(并与SQLite同步)来自Firebase的新数据或更改的数据。 在第一种情况下,我想检查借书的状态是否发生了变化。因此,我在onStartCommand中使用以下代码: databaseLoanedBooks.orderByChild("contactId").equalTo(firebase_uid).addChildEventListener(new ChildEventListener() { @
databaseLoanedBooks.orderByChild("contactId").equalTo(firebase_uid).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String book_id = dataSnapshot.child("bookId").getValue(String.class);
String book_status = dataSnapshot.child("bookStatus").getValue(String.class);
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
List<String> all_book_ids = db.getAllBookIds();
if (all_book_ids.contains(book_id)){
// some action... check for status, if status=x output of notification
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
databaseLoanedBooks.orderByChild(“contactId”).equalTo(firebase\u uid).addChildEventListener(new ChildEventListener()){
@凌驾
公共void onChildAdded(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
公共void onChildChanged(DataSnapshot DataSnapshot,字符串s){
String book_id=dataSnapshot.child(“bookId”).getValue(String.class);
String book_status=dataSnapshot.child(“bookStatus”).getValue(String.class);
DatabaseHelper db=新的DatabaseHelper(getApplicationContext());
列出所有的书籍ID=db.getAllBookID();
if(所有图书标识包含(图书标识)){
//某些操作…检查状态,如果状态=通知的x输出
}
}
@凌驾
ChildRemoved上的公共void(DataSnapshot DataSnapshot){
}
@凌驾
已移动ChildMoved上的公共void(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
此外,我想检查服务器上是否有关于借书的新消息。因此,我在NotifiactionListener的onStart命令中使用以下代码:
final List<String> all_book_ids = db.getAllBookIds();
// iterate through all book-ids the user has loaned at the moment (and saved in SQL)
for (int i = 0; i < all_book_ids .size(); i++) {
final String check_book_id = all_book_ids.get(i);
databaseMessage.child(check_book_id).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ChatMessage chatMessage = dataSnapshot.getValue(ChatMessage.class);
String message_id = dataSnapshot.child("messageId").getValue(String.class);
String message_book_id = dataSnapshot.child("messageBookId").getValue(String.class);
String message_uid = dataSnapshot.child("messageUid").getValue(String.class);
String message_text = dataSnapshot.child("messageText").getValue(String.class);
String message_time = String.valueOf(chatMessage.getMessageTime());
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
final List<String> all_message_ids = db.getAllMessagesIds();
final List<String> all_book_ids = db.getAllBookIds();
String new_message = getString(R.string.text_new_message);
if (all_book_ids.contains(message_book_id)) {
if (all_message_ids.contains(message_id_changed)) {
// already exists
} else {
ContentValues values_new = new ContentValues();
values_new.put(DatabaseHelper.COLUMN_CHAT_FCM_ID, message_id); values_new.put(DatabaseHelper.COLUMN_CHAT_BOOK_ID, message_book_id);
values_new.put(DatabaseHelper.COLUMN_CHAT_TEXT, message_text_changed); values_new.put(DatabaseHelper.COLUMN_CHAT_BOOK_ID, message_uid);
values_new.put(DatabaseHelper.COLUMN_CHAT_TIME, message_time);
todoUri = getContentResolver().insert(DataContentProvider.CONTENT_URI_CHATS, values_new);
HashMap<String, String> user_session = session.getUserDetails();
// UID from sharedpreferences
String noti_status = user_session.get(SessionManager.KEY_NOTIFICATION);
String user_name_chat_sql = String.valueOf(db.getContactName(message_uid));
String book_name = String.valueOf(db.getBookNameForWA(message_book_id));
user_name_chat_sql = user_name_chat_sql.replaceAll("\\p{P}", "");
book_name = book_name.replaceAll("\\p{P}", "");
showNotificationNewMessage(new_message, user_name_chat_sql, message_text, book_name);
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
final List all_book_id=db.getAllBookIds();
//遍历用户目前借出(并保存在SQL中)的所有图书ID
for(int i=0;i
使用这段代码,每个用户每天的数据使用量大约为2MB。所以我的问题是:我怎样才能最大限度地减少我的下载量,并从中跟踪我的数据使用情况?在检查新消息的代码中,我尝试使用for循环来限制仅为相应用户的书籍收集的数据,但不幸的是,这不是解决方案。如何解决此问题?请添加您的数据库结构。