2016-09-06 45 views
1

我有一個使用RxJava和Retrofit2加載數據的活動。爲防止每次發生配置更改時觸發請求,我使用cache()運算符。這工作得很好,我可以看到請求只做了一次。問題在於,我不時需要提出新的要求以獲取最新的數據。換句話說,我需要使緩存無效。我怎樣才能做到這一點?RxJava和Android:如何使使用cache()的observable無效?

的活性是使用存儲庫發出請求:

public class Repository { 
private final PlaceholderApi api; 
private Observable<List<Post>> obs; 
private static Repository inst; 

public static Repository instance() { 
    if (inst == null) { 
     inst = new Repository(); 
    } 
    return inst; 
} 

private Repository() { 
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> System.out.println(message)); 
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC); 
    OkHttpClient okhttp = new OkHttpClient.Builder() 
      .addInterceptor(logging) 
      .build(); 
    Retrofit retrofit = new Retrofit.Builder() 
      .client(okhttp) 
      .addConverterFactory(MoshiConverterFactory.create()) 
      .baseUrl("http://jsonplaceholder.typicode.com/") 
      .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) 
      .build(); 
    api = retrofit.create(PlaceholderApi.class); 
} 

public Observable<List<Post>> getAll() { 
    if (obs == null) { 
     obs = api.getPosts().cache(); 
    } 
    return obs; 
} 
} 

而且活動代碼:

public class MainActivity extends AppCompatActivity { 

private static final String TAG = "MainActivity"; 
private Subscription sub1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    sub1 = Repository.instance().getAll().subscribe(/* handle posts list */); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (sub1 != null && !sub1.isUnsubscribed()) { 
     sub1.unsubscribe(); 
    } 
} 
} 
+0

查看http://stackoverflow.com/questions/31733455/rxjava-observable-cache-invalidate –

+0

@DaveMoten,感謝您的鏈接,但如何做到這一點與Retrofit? –

回答

0

下面是使用來自Dave MOTEN的答案OnSubscribeRefreshingCache包裝類的例子 - https://stackoverflow.com/a/31738646/6491552你代碼:

public class MainActivity extends AppCompatActivity { 

private static final String TAG = "MainActivity"; 
private Subscription sub1; 
private OnSubscribeRefreshingCache<List<Post>> postCache; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     postCache = new OnSubscribeRefreshingCache(Repository.instance().getAll()); 
     sub1 = subscribe(Observable.create(postCache)); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     if (sub1 != null && !sub1.isUnsubscribed()) { 
      sub1.unsubscribe(); 
     } 
    } 

    private Subscription subscribe(Observable<List<Post> postObservable){ 
     return postObservable.subscribe(/* handle posts list */); 
    } 

    private void invalidateCache(){ 
     postCache.reset(); 
     sub1 = subscribe(Observable.create(postCache)); 
    } 
} 

然後調用invalidateCache()方法在一個Activity生命週期方法或任何適用於您的方法中。

0

解決您的問題的另一種方法是使用OperatorFreeze(無操作員緩存),如果您使用持久性主持人。此操作符可以在發生配置更改時暫停Observable,並在重新創建活動時恢復它。您也不需要將Observables存儲在存儲庫中。