我遇到了一个问题,随机我将停止接收所有应用观察者的事件。我终于发现这种情况发生在两种情况下。网络中断或应用程序被发送到后台。我尝试使用class Order {
static let instance: Order = Order()
var orders: [DataSnapshot] = []
func watchOrders() {
let orderRef = database.child("orders/\(AppData.instance.currentStore!.key)")
orderRef.observe(.value, with: { snapshot -> Void in
self.orders.removeAll()
let orders = snapshot.children
while let order = orders.nextObject() as? DataSnapshot {
self.orders.append(order)
}
self.updateOrderViews()
})
}
func updateOrderViews() {
if let tillView = AppViews.instance.tillView {
tillView.tillTable.reloadData()
}
if let tillFunctionView = AppViews.instance.tillFunctionView {
tillFunctionView.calcTill()
}
if let tillReconcileView = AppViews.instance.tillReconcileView {
tillReconcileView.calcTill()
}
if let dispatchView = AppViews.instance.dispatchView {
dispatchView.loadDispatch()
}
if let dispatchRecapView = AppViews.instance.dispatchRecapView {
dispatchRecapView.getDispatchedOrders()
}
if let managerView = AppViews.instance.managerView {
managerView.getReport()
}
if let searchView = AppViews.instance.searchView {
searchView.filterOrders()
}
}
}
无济于事。还有其他方法可以恢复我已注册的观察员吗?
当我的程序启动时,它会调用几个具有观察属于它们的数据的函数的实例。然后将来自观察者的数据存储在实例中,然后如果它们当前存在则更新视图。通过这种方式,数据将在后台更新,我可以在视图控制器之间移动而无需加载时间。似乎这些观察者正在超时或类似。
package com.google.android.gms.example.interstitialexample;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import android.util.*;
public class MyActivity extends ActionBarActivity {
private static final long GAME_LENGTH_MILLISECONDS = 3000;
private InterstitialAd mInterstitialAd;
private CountDownTimer mCountDownTimer;
private Button mRetryButton;
private boolean mGameIsInProgress;
private long mTimerMilliseconds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, "app-id");
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
startGame();
}
});
// Create the "retry" button, which tries to show an interstitial between game plays.
mRetryButton = ((Button) findViewById(R.id.retry_button));
mRetryButton.setVisibility(View.INVISIBLE);
mRetryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInterstitial();
}
});
startGame();
}
private void createTimer(final long milliseconds) {
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
final TextView textView = ((TextView) findViewById(R.id.timer));
mCountDownTimer = new CountDownTimer(milliseconds, 50) {
@Override
public void onTick(long millisUnitFinished) {
mTimerMilliseconds = millisUnitFinished;
textView.setText("seconds remaining: " + ((millisUnitFinished / 1000) + 1));
}
@Override
public void onFinish() {
mGameIsInProgress = false;
textView.setText("done!");
mRetryButton.setVisibility(View.VISIBLE);
}
};
}
@Override
public void onResume() {
// Start or resume the game.
super.onResume();
if (mGameIsInProgress) {
resumeGame(mTimerMilliseconds);
}
}
@Override
public void onPause() {
// Cancel the timer if the game is paused.
mCountDownTimer.cancel();
super.onPause();
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
new Thread(new Runnable(){
public void run() {
for(int i=0;i<1000;i++){
Log.d("MainActivity", "BG Thread"+i);
try{
Thread.sleep(2000);
}catch(InterruptedException e){
}
}
}
}).start();
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
startGame();
}
}
private void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
mRetryButton.setVisibility(View.INVISIBLE);
resumeGame(GAME_LENGTH_MILLISECONDS);
}
private void resumeGame(long milliseconds) {
// Create a new timer for the correct length and start it.
mGameIsInProgress = true;
mTimerMilliseconds = milliseconds;
createTimer(milliseconds);
mCountDownTimer.start();
}