Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Michael Nitschinger
    @daschl
    latest one for every contact
    Javier Domingo Cansino
    @txomon
    indeed, this is now the second layer
    Michael Nitschinger
    @daschl
    well, how many contacts do you have? because if you store aggregated list, this could get huuuge right?
    Javier Domingo Cansino
    @txomon
    I need to hold them anyway, so it doens't matter
    anyway, this is going to be a listview later in the app
    Michael Nitschinger
    @daschl
    well, there is a way to do it
    remember, there is always a way in rx :D
    Javier Domingo Cansino
    @txomon
    yeah
    I was thinking on distinct
    Michael Nitschinger
    @daschl
    so then before the cache() (which is your last operator then), you add
    Javier Domingo Cansino
    @txomon
    btw, I think the documentation is incomplete...
    Michael Nitschinger
    @daschl
    no, you want scan()
    Javier Domingo Cansino
    @txomon
    documented I mean
    Michael Nitschinger
    @daschl
    sorry I meant replay instead of cache for the latest
    but before that put scan
    initialize it with an empty Map (or set) and then on every call you get access to it and add it
    then it will be emitted
    Javier Domingo Cansino
    @txomon
    hmmm ok
    Michael Nitschinger
    @daschl
    check out the docs for it.. scan is normally used to add values for example, but since the Map will be stored as state
    its pretty neat
    let me see maybe I can show it quickly
    Javier Domingo Cansino
    @txomon
    I will be using just a list
    Michael Nitschinger
    @daschl
    don’t use a list
    Javier Domingo Cansino
    @txomon
    I don't really need a Map, do I?
    why?
    Michael Nitschinger
    @daschl
    well, you only want the last version of each
    if you append to a list you get an ever growing list (unless you cut it down) of changes
    Javier Domingo Cansino
    @txomon
    yes, so I remove the item and append the new one
    Michael Nitschinger
    @daschl
    I thought you only want the latest version of each contact
    Javier Domingo Cansino
    @txomon
    that way I can show them by update time
    Michael Nitschinger
    @daschl
    try this
        public static void main(String... args) throws Exception {
            Observable
              .interval(1, TimeUnit.SECONDS)
              .map(num -> "user" + (num%5) + "-v" + num)
              .scan(new HashMap<>(), (map, value) -> {
                  String[] info = value.split("-");
                  map.put(info[0], info[1]);
                  return map;
              })
              .filter(map -> !map.isEmpty())
              .toBlocking()
              .forEach(System.out::println);
    Javier Domingo Cansino
    @txomon
        return Observable.concat(
                contactRepository.getContactList().flatMap((a) -> Observable.from(a)),
                contactRepository.getContactUpdates()
        ).scan(
                new ArrayList<Contact>(),
                (list, contact) -> {
                    for(Contact contactInList : list){
                        if(contactInList.getUserId() == contact.getUserId()){
                            list.remove(contactInList);
                        }
                    }
                    list.add(contact);
                    return list;
                }
        );
    Michael Nitschinger
    @daschl
    in my sample code, you’ll see how the map builds up and then refreshes:
    {user0=v0}
    {user1=v1, user0=v0}
    {user1=v1, user2=v2, user0=v0}
    {user1=v1, user2=v2, user0=v0, user3=v3}
    {user1=v1, user2=v2, user0=v0, user3=v3, user4=v4}
    {user1=v1, user2=v2, user0=v5, user3=v3, user4=v4}
    {user1=v6, user2=v2, user0=v5, user3=v3, user4=v4}
    {user1=v6, user2=v7, user0=v5, user3=v3, user4=v4}
    {user1=v6, user2=v7, user0=v5, user3=v8, user4=v4}
    {user1=v6, user2=v7, user0=v5, user3=v8, user4=v9}
    {user1=v6, user2=v7, user0=v10, user3=v8, user4=v9}
    Javier Domingo Cansino
    @txomon
    hmmm
    Michael Nitschinger
    @daschl
    so it always keeps the latest state emitted for the object
    @txomon your for loop has o(N) complexity while the hash replace is just O(1)
    Javier Domingo Cansino
    @txomon
    @daschl yeah, but I need it in order
    Michael Nitschinger
    @daschl
    okay, then maybe use a queue where you can push/pop?
    Javier Domingo Cansino
    @txomon
    but items are not updated in order
    Michael Nitschinger
    @daschl
    so what order do you need?
    Javier Domingo Cansino
    @txomon
    yes, I have just realized I haven't described the app
    Michael Nitschinger
    @daschl
    Note that there is also a TreeMap which sorts the keys by its Comparator
    Javier Domingo Cansino
    @txomon
    the app is meant to be a view of your contacts, where instead of being ordered by alphabetical order or so, it's ordered by the latest updated
    such as the call app in android, which shows the recently contacted ones
    Michael Nitschinger
    @daschl
    @txomon implement the comparable on that property and use a https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
    so your set will be ordered by the update date