Collect all data from FirebaseDatabse and call notifyDataSetChanged() once
in brief: I have a list of users ID and I want to iterate over database and find profiles of those users and put them on the list. But I have a problem as follows:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
// The problem is here, because its called as many times as the size of
// the friendIds list. loadnewData() contains notifyDataSetChanged()
mFriendsFragment.loadNewData(friendsProfiles);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// It gives 0, because it's called before onDatachange()
// So I can't call loadNewData() here
Log.d(TAG, updatedFriendsRequestList.size());
How to do it in the nice, proper way?
android firebase firebase-realtime-database android-recyclerview recycler-adapter
add a comment |
in brief: I have a list of users ID and I want to iterate over database and find profiles of those users and put them on the list. But I have a problem as follows:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
// The problem is here, because its called as many times as the size of
// the friendIds list. loadnewData() contains notifyDataSetChanged()
mFriendsFragment.loadNewData(friendsProfiles);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// It gives 0, because it's called before onDatachange()
// So I can't call loadNewData() here
Log.d(TAG, updatedFriendsRequestList.size());
How to do it in the nice, proper way?
android firebase firebase-realtime-database android-recyclerview recycler-adapter
What is the type of yourfriendIds
list?
– Alex Mamo
Nov 13 '18 at 18:04
I simplified the case in sake of better understanding. In original it contains: String friendId and type of the invitation to friends ("sent" or "received"). So it is basically list of objects that contain 2 strings.
– northenofca
Nov 14 '18 at 5:01
So basically yourfriendIds
list is declared asList<String> friendIds = new ArrayList<>();
, right?
– Alex Mamo
Nov 14 '18 at 8:42
No. In this case it is a list of invitations to the friends. It looks like this:List<FriendRequest> requests= new ArrayList<>();
where FriendRequest is an object that containsString frienId
andString requestType
("sent" or "received") either he sent invitation or received and this determines which layout to use.
– northenofca
Nov 14 '18 at 9:55
Thanks for your explanation. One more thing, please show me how yourfriendIds
list is declared. Thanks!
– Alex Mamo
Nov 14 '18 at 11:04
add a comment |
in brief: I have a list of users ID and I want to iterate over database and find profiles of those users and put them on the list. But I have a problem as follows:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
// The problem is here, because its called as many times as the size of
// the friendIds list. loadnewData() contains notifyDataSetChanged()
mFriendsFragment.loadNewData(friendsProfiles);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// It gives 0, because it's called before onDatachange()
// So I can't call loadNewData() here
Log.d(TAG, updatedFriendsRequestList.size());
How to do it in the nice, proper way?
android firebase firebase-realtime-database android-recyclerview recycler-adapter
in brief: I have a list of users ID and I want to iterate over database and find profiles of those users and put them on the list. But I have a problem as follows:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
// The problem is here, because its called as many times as the size of
// the friendIds list. loadnewData() contains notifyDataSetChanged()
mFriendsFragment.loadNewData(friendsProfiles);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// It gives 0, because it's called before onDatachange()
// So I can't call loadNewData() here
Log.d(TAG, updatedFriendsRequestList.size());
How to do it in the nice, proper way?
android firebase firebase-realtime-database android-recyclerview recycler-adapter
android firebase firebase-realtime-database android-recyclerview recycler-adapter
edited Nov 13 '18 at 17:56
Frank van Puffelen
230k28376400
230k28376400
asked Nov 13 '18 at 16:05
northenofcanorthenofca
135
135
What is the type of yourfriendIds
list?
– Alex Mamo
Nov 13 '18 at 18:04
I simplified the case in sake of better understanding. In original it contains: String friendId and type of the invitation to friends ("sent" or "received"). So it is basically list of objects that contain 2 strings.
– northenofca
Nov 14 '18 at 5:01
So basically yourfriendIds
list is declared asList<String> friendIds = new ArrayList<>();
, right?
– Alex Mamo
Nov 14 '18 at 8:42
No. In this case it is a list of invitations to the friends. It looks like this:List<FriendRequest> requests= new ArrayList<>();
where FriendRequest is an object that containsString frienId
andString requestType
("sent" or "received") either he sent invitation or received and this determines which layout to use.
– northenofca
Nov 14 '18 at 9:55
Thanks for your explanation. One more thing, please show me how yourfriendIds
list is declared. Thanks!
– Alex Mamo
Nov 14 '18 at 11:04
add a comment |
What is the type of yourfriendIds
list?
– Alex Mamo
Nov 13 '18 at 18:04
I simplified the case in sake of better understanding. In original it contains: String friendId and type of the invitation to friends ("sent" or "received"). So it is basically list of objects that contain 2 strings.
– northenofca
Nov 14 '18 at 5:01
So basically yourfriendIds
list is declared asList<String> friendIds = new ArrayList<>();
, right?
– Alex Mamo
Nov 14 '18 at 8:42
No. In this case it is a list of invitations to the friends. It looks like this:List<FriendRequest> requests= new ArrayList<>();
where FriendRequest is an object that containsString frienId
andString requestType
("sent" or "received") either he sent invitation or received and this determines which layout to use.
– northenofca
Nov 14 '18 at 9:55
Thanks for your explanation. One more thing, please show me how yourfriendIds
list is declared. Thanks!
– Alex Mamo
Nov 14 '18 at 11:04
What is the type of your
friendIds
list?– Alex Mamo
Nov 13 '18 at 18:04
What is the type of your
friendIds
list?– Alex Mamo
Nov 13 '18 at 18:04
I simplified the case in sake of better understanding. In original it contains: String friendId and type of the invitation to friends ("sent" or "received"). So it is basically list of objects that contain 2 strings.
– northenofca
Nov 14 '18 at 5:01
I simplified the case in sake of better understanding. In original it contains: String friendId and type of the invitation to friends ("sent" or "received"). So it is basically list of objects that contain 2 strings.
– northenofca
Nov 14 '18 at 5:01
So basically your
friendIds
list is declared as List<String> friendIds = new ArrayList<>();
, right?– Alex Mamo
Nov 14 '18 at 8:42
So basically your
friendIds
list is declared as List<String> friendIds = new ArrayList<>();
, right?– Alex Mamo
Nov 14 '18 at 8:42
No. In this case it is a list of invitations to the friends. It looks like this:
List<FriendRequest> requests= new ArrayList<>();
where FriendRequest is an object that contains String frienId
and String requestType
("sent" or "received") either he sent invitation or received and this determines which layout to use.– northenofca
Nov 14 '18 at 9:55
No. In this case it is a list of invitations to the friends. It looks like this:
List<FriendRequest> requests= new ArrayList<>();
where FriendRequest is an object that contains String frienId
and String requestType
("sent" or "received") either he sent invitation or received and this determines which layout to use.– northenofca
Nov 14 '18 at 9:55
Thanks for your explanation. One more thing, please show me how your
friendIds
list is declared. Thanks!– Alex Mamo
Nov 14 '18 at 11:04
Thanks for your explanation. One more thing, please show me how your
friendIds
list is declared. Thanks!– Alex Mamo
Nov 14 '18 at 11:04
add a comment |
1 Answer
1
active
oldest
votes
You can simply count how many you've already loaded, and then call notifyDataSetChanged()
only once you've loaded the last one:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
if (friendsProfiles.size() == friendIds.length) {
mFriendsFragment.loadNewData(friendsProfiles);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors, as they break the logic of your app
}
});
}
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284983%2fcollect-all-data-from-firebasedatabse-and-call-notifydatasetchanged-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can simply count how many you've already loaded, and then call notifyDataSetChanged()
only once you've loaded the last one:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
if (friendsProfiles.size() == friendIds.length) {
mFriendsFragment.loadNewData(friendsProfiles);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors, as they break the logic of your app
}
});
}
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
|
show 2 more comments
You can simply count how many you've already loaded, and then call notifyDataSetChanged()
only once you've loaded the last one:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
if (friendsProfiles.size() == friendIds.length) {
mFriendsFragment.loadNewData(friendsProfiles);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors, as they break the logic of your app
}
});
}
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
|
show 2 more comments
You can simply count how many you've already loaded, and then call notifyDataSetChanged()
only once you've loaded the last one:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
if (friendsProfiles.size() == friendIds.length) {
mFriendsFragment.loadNewData(friendsProfiles);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors, as they break the logic of your app
}
});
}
You can simply count how many you've already loaded, and then call notifyDataSetChanged()
only once you've loaded the last one:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
if (friendsProfiles.size() == friendIds.length) {
mFriendsFragment.loadNewData(friendsProfiles);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors, as they break the logic of your app
}
});
}
edited Nov 13 '18 at 17:56
answered Nov 13 '18 at 17:22
Frank van PuffelenFrank van Puffelen
230k28376400
230k28376400
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
|
show 2 more comments
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
If the list contains 1000 ids, is it ok to add a listener 1000 times?
– Ioana P.
Nov 13 '18 at 17:23
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
That seems like a different question. But all your listeners are short-lived, so I doubt it'll create a problem. If it does (or you're worried enough you want to be sure it doesn't), throttle how often you add a next listener: so add the first 10 or so, and then only add the next one once another completes (or gets cancelled).
– Frank van Puffelen
Nov 13 '18 at 17:26
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
I've got the answer to one of my biggest problems in my today's work from this comment. Thank you!!!! I'll try your solution. +1
– Ioana P.
Nov 13 '18 at 17:33
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@IoanaP. I know it's awful solution, but I can not find better one. What do you suggest?
– northenofca
Nov 13 '18 at 17:35
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
@Frank van Puffelen Thank you, it's simple solution, why it did not come to my mind.. But I will wait, maybe someone post another solution.
– northenofca
Nov 13 '18 at 17:36
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284983%2fcollect-all-data-from-firebasedatabse-and-call-notifydatasetchanged-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the type of your
friendIds
list?– Alex Mamo
Nov 13 '18 at 18:04
I simplified the case in sake of better understanding. In original it contains: String friendId and type of the invitation to friends ("sent" or "received"). So it is basically list of objects that contain 2 strings.
– northenofca
Nov 14 '18 at 5:01
So basically your
friendIds
list is declared asList<String> friendIds = new ArrayList<>();
, right?– Alex Mamo
Nov 14 '18 at 8:42
No. In this case it is a list of invitations to the friends. It looks like this:
List<FriendRequest> requests= new ArrayList<>();
where FriendRequest is an object that containsString frienId
andString requestType
("sent" or "received") either he sent invitation or received and this determines which layout to use.– northenofca
Nov 14 '18 at 9:55
Thanks for your explanation. One more thing, please show me how your
friendIds
list is declared. Thanks!– Alex Mamo
Nov 14 '18 at 11:04