便宜上、各ビューに変数/参照を必要とせずに待機インジケータを表示できるヘルパークラスを使用しています。クラスIはDFActivity指標を作成し、パラメータで指定されたビューに表示しています、この方法では、静的メソッド公共静的メソッドで初期化され、NSNotificationCenterを使用してオブジェクトが初期化されたメモリリーク
static void ShowActivityIndicator(UIView view, bool animated, UIActivityIndicatorViewStyle style)
を実装:
DFActivityIndicator activityIndicator = new DFActivityIndicator(view.Bounds);
view.AddSubview(activityIndicator);
activityIndicator.LabelText = NSBundle.MainBundle.LocalizedString("Loading...", "");
activityIndicator.Indicator.ActivityIndicatorViewStyle = style;
activityIndicator.Show(true);
方法のコンストラクタは次のとおりです。
public DFActivityIndicator(RectangleF frame) : base(frame)
{
Indicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
this.AddSubview(Indicator);
Indicator.StartAnimating();
Indicator.Frame = new RectangleF(0.0f, 0.0f, 20.0f, 20.0f);
Indicator.StartAnimating();
//...
Label = new UILabel(Bounds);
RotationTransform = CGAffineTransform.MakeIdentity();
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidChangeStatusBarOrientationNotification, DeviceOrientationDidChange, this);
}
ここで、インタフェースを回転させると、オブザーバはインジケータを回転させることができます。インジケータはもはや必要ないときは、私は他の静的メソッドがあります。これは正常に動作している
public static bool HideActivityIndicator(UIView view, bool animated)
{
UIView viewToRemove = null;
foreach(UIView v in view.Subviews)
{
if (v is DFActivityIndicator)
{
viewToRemove = v;
}
}
if (viewToRemove != null)
{
DFActivityIndicator activityIndicator = viewToRemove as DFActivityIndicator;
NSNotificationCenter.DefaultCenter.RemoveObserver(activityIndicator, UIApplication.DidChangeStatusBarOrientationNotification, null);
activityIndicator.RemoveFromSuperview();
activityIndicator.Dispose();
activityIndicator = null;
return true;
}
else
{
return false;
}
}
、モノプロファイラは、私がShowActivityIndicator
を呼び出していますたびに、各インスタンスがメモリに保持されていることを示し期待しても、Iすべてのインスタンスに対してHideActivityIndicator
と呼び出してください。私のアプリケーションのメモリは、クラッシュするまで増加しています(メモリリークのようです)。デバッグのために、オリエンテーションの変更でオブザーバを削除しようとしました: NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidChangeStatusBarOrientationNotification, DeviceOrientationDidChange, this)
) と...コードがもう漏れていません。 MonoTouchのバグですか、それとも何か間違っていますか?
私の推測では、私は右AddObserver/RemoveObserverメソッドを使用していないです。 – nicolas
HideActivityIndicatorでオブザーバを削除しても機能しないと言っていますが、オリエンテーションを変更するとオブザーバを削除すると効果がありますか? HeapShotプロファイラは、「逆参照」チェックボックスをクリックすると、どのオブジェクトがDFActivityIndicatorを有効にしているかを示すこともできます。 –
「AddObserver」と「RemoveObserver」という行にコメントしているときは、もうリークがないようです。 'DFActivityIndicator'を生かし続けるようなリファレンスは、' System.Collection.Generic.List 'によって保持される' MonoTouch.Foundation.InternalNSNotificationHandler'によって保持される 'System.Action 'であり、 'MonoTouch .Foundation.NSNotificationCenter'。 –
nicolas