简体   繁体   English

Android应用程序架构 - RxJava

[英]Android app architecture - RxJava

I am working on an android app (basically trying to get my head around using RxJava/Android). 我正在开发一个Android应用程序(基本上试图让我的头脑使用RxJava / Android)。 These are my requirements: 这些是我的要求:

  • Maintain a persistent and global state for the logged in user 为登录用户维护持久和全局状态
  • Whenever the user state changes, update views accordingly. 每当用户状态发生更改时,都会相应地更新视图。

I am using a singleton User instance to maintain global state and SharedPreferences for persistence. 我使用单例User实例来维护全局状态和SharedPreferences以保持持久性。 Now I'm trying to use RxAndroid for notifying changes to the views, but I'm clueless on how to go about that. 现在,我正在尝试使用RxAndroid来通知视图的更改,但我对如何解决这个问题一无所知。

How do we track the changes in User ? 我们如何跟踪User的变化? Can I use the setters in User to emit from the observables; 我可以使用User的setter从observable发出; if so, how do I go about it? 如果是的话,我该怎么做呢?

I would really appreciate if anyone can provide code samples too. 如果有人也可以提供代码示例,我将非常感激。

PS This is a sample of my User class definition, I haven't used any setters. PS这是我的User类定义的示例,我没有使用任何setter。

public class User {
  public String userName;
  public String emailId;
  public List<Friend> friends;

  public User() {
    friends = new ArrayList<Friend>();
  }
}

public class Friend {
  public String userName;
  public String emailId;
  public String role;
}

This is just a sample skeleton of my User class (I have stripped off everything else), I just need to know how the setters are going to look like for these fields, and if this approach is even considered right or if there are better alternatives. 这只是我的User类的示例骨架(我已经剥离了其他所有东西),我只需要知道这些字段的setter看起来如何,如果这种方法被认为是正确的,或者是否有更好的选择。

Please let me know if you need more information. 如果您需要更多信息,请与我们联系。

i would use PublishSubject http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html from RxJava and onResume/onPause of Activtiy subscribe/unsubsribe from it. 我将使用来自RxJava的PublishSubject http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html和来自它的Activtiy subscribe / unsubsribe的onResume / onPause。

create PublishSubject inside User 在User中创建PublishSubject

public class User {
  public PublishSubject<User> stream = PublishSubject.create()
  public String userName;
  public String emailId;
  public List<Friend> friends;

  public User() {
    friends = new ArrayList<Friend>();
  }
  public void setName(String name){
         stream.onNext(this);
  }
}

So when you will use method setName of User class (Singleton as you mentioned) every thing subscribed to that steam will recive answer. 因此,当您使用User类的方法setName(如您所提到的Singleton)时,订阅该Steam的每个东西都会回复。 like 喜欢

 public class MyActivity extends Activity{

            private TextView nameTextView ;
            private Subscription subs;
            @Override
            public void onCreate{
            //initialization of fields
            }

            @Override
            public void onResume(){
              super.onResume();
              subs = User.getInstance().stream.subscribe{user-> nameTextView.setText(user.username) }
          }

             @Override
             public void onPause(){
             super.onPause();
             subs.unsubscribe();
             }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM