2017-12-22 20 views
0

今日はウィジェットの拡張機能を作りました。コインのシンボルを取得してから、オブジェクトを取得するためのWebリクエストをしています。Xamarin-iOS本日のウィジェット、ロードできない問題

すべてのリロードで、ウィジェットはもう一度ビューを描画し、1/10ウィジェットの表示から私がロードできないWidgetPerformUpdateメソッドを呼び出しています。

私はウィジェットにいくつかのオブジェクトを追加しているときに私はアプリを暴露し、問題が私に来ています。

私はこの問題を1週間ほど積み重ねてきました。今日のウィジェットのドキュメント、チュートリアルはすべて読んだことがありません。

私は何をすべきか分かりません。負荷がかからないのは私の悪夢です。

TodawyWidgetViewControllerコードがあります。

using System; 
using System.Collections.Generic; 
using NotificationCenter; 
using Foundation; 
using UIKit; 
using CryptoCurrencyPCL.POCO; 
using CryptoCurrencyPCL.Middleware; 
using System.Linq; 
using System.Threading.Tasks; 
using CryptoCurrencyPCL.Extensions; 
using CryptoCurrencyPCL.Enums; 
using CoreGraphics; 
using Newtonsoft; 
using Newtonsoft.Json; 
using CryptoPCL.POCO; 

namespace CryptoTodayWidget 
{ 
    public partial class TodayViewController : UIViewController, INCWidgetProviding,IUITableViewDataSource,IUITableViewDelegate 
    { 
     NSUserDefaults userDefaults; 
     const string ReuseId = "currencyCellReuseId"; 
     List<CoinDetail> _coins; 
     List<CoinDetail> _cachedCoins; 
     List<FavoriteCoin> _favorites; 
     List<string> listOfStrings = new List<string>(); 
     Currency itemCurrency; 
     private CGSize _maxSize; 
     private string nsStringForValues; 
     private string nsString; 
     private List<string> deserializedObjectsSymbols; 
     bool _firstInit = true; 


     protected TodayViewController(IntPtr handle) : base(handle) 
     { 
      // Note: this .ctor should not contain any initialization logic. 
     } 

     public override void DidReceiveMemoryWarning() 
     { 
      // Releases the view if it doesn't have a superview. 
      base.DidReceiveMemoryWarning(); 

      // Release any cached data, images, etc that aren't in use. 
     } 

     public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
     { 
      var cell = tableView.DequeueReusableCell(ReuseId, indexPath) as WidgetCell; 

      var item = _coins[indexPath.Row]; 
      var favorite = _favorites[indexPath.Row]; 

      cell.InitData(item,favorite,itemCurrency); 

      return cell; 
     } 

     public nint RowsInSection(UITableView tableView, nint section) 
     { 
      return _coins?.Count ?? 0; 
     } 

     [Export("tableView:heightForRowAtIndexPath:")] 
     public nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath) 
     { 
      return 50; 
     } 

     [Export("numberOfSectionsInTableView:")] 
     public nint NumberOfSections(UITableView tableView) 
     { 
      return 1; 
     } 

     public async override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      var webClient = CryptoCurrencyPCL.Services.CryptoWebClient.Instance; 
      tableView.SeparatorStyle = UITableViewCellSeparatorStyle.None; 

      _cachedCoins = _coins; 
      if(_coins != null) { 
       initTableView(); 
      } 

      ExtensionContext.SetWidgetLargestAvailableDisplayMode(NCWidgetDisplayMode.Expanded); 
     } 

     private void initTableView() 
     { 
      tableView.DataSource = this; 
      tableView.Delegate = this; 
      tableView.AllowsSelection = false; 
      tableView.ReloadData(); 
     } 

     public override void ViewDidAppear(bool animated) 
     { 
      base.ViewDidAppear(animated); 

     } 

     [Export("widgetPerformUpdateWithCompletionHandler:")] 
     public async void WidgetPerformUpdate(Action<NCUpdateResult> completionHandler) 
     { 
      var webClient = CryptoCurrencyPCL.Services.CryptoWebClient.Instance; 



      try 
      { 
       initLoading(); 
       bool check = false; 
       userDefaults = new NSUserDefaults("group.com.mpdc.todayextension", NSUserDefaultsType.SuiteName); 
       nsStringForValues = userDefaults?.StringForKey(new NSString("MyAppsValues")); 
       nsString = userDefaults?.StringForKey("currencyKey"); 
       itemCurrency = (Currency)Enum.Parse(typeof(Currency), nsString); 

       _favorites = JsonConvert.DeserializeObject<List<FavoriteCoin>>(nsStringForValues); 

       deserializedObjectsSymbols = _favorites.Select(o => o.FavoriteCoinSymbol).ToList(); 


       _coins = await webClient.GetMultiCoinDetailsAsync(deserializedObjectsSymbols, itemCurrency); 


       if (ExtensionContext.GetWidgetLargestAvailableDisplayMode() == NCWidgetDisplayMode.Compact) 
       { 

        this.PreferredContentSize =_maxSize; 
       } 

       else 
       { 
        this.PreferredContentSize = new CoreGraphics.CGSize(0, _coins.Count * 50); 
       } 

       initTableView(); 
       initLoading(false); 

       tableView.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine; 

       completionHandler(NCUpdateResult.NewData); 

      } 
      catch (Exception ex) 
      { 
       initLoading(false); 
       completionHandler(NCUpdateResult.Failed); 
       Console.WriteLine(ex); 
      } 

     } 


     void initLoading(bool visible = true) 
     { 

      if (visible) 
      { 
       ActivityIndicator.Hidden = false; 
       ActivityIndicator.StartAnimating(); 
       return; 
      } 

      ActivityIndicator.Hidden = true; 
      ActivityIndicator.StopAnimating(); 
     } 

     [Export("widgetActiveDisplayModeDidChange:withMaximumSize:")] 
     public void WidgetActiveDisplayModeDidChange(NCWidgetDisplayMode activeDisplayMode, CoreGraphics.CGSize maxSize) 
     { 
      _maxSize = maxSize; 

      if (activeDisplayMode == NCWidgetDisplayMode.Compact) 
      { 

       this.PreferredContentSize = _maxSize; 
      } 

      else 
      { 
       if(_coins!=null) 
       this.PreferredContentSize = new CoreGraphics.CGSize(0, _coins.Count * 50); 
      } 
     } 

    } 
} 

は、私はそれを解決し、あなたに

+0

問題を確認するためにクラッシュコードを送信することができます。たぶん「CryptoCurrencyPCL」の問題か、データをロードするのに時間がかかりすぎるかもしれません。今日の拡張機能は長時間の作業には使用しないでください。 –

+0

非常に奇妙な問題は、実際のデバイスではなく、シミュレータでの作業であることです –

+0

シミュレータと実際のデバイスの間にいくつかの違いがあります。たぶん、データが多すぎるし、データの量を減らして再度デバッグしようとするかもしれません。 –

答えて

0

ありがとうございます。

シミュレータでは動作していましたが、デバイスでは動作しませんでした。

デバッグモードのデバイスでアプリケーションを実行しているときに問題が発生していました。

デバッグモードでは、メモリの問題が発生していると思います。

リリースモードで実行しましたが、すべてが機能しています。

関連する問題