2012-08-09 104 views
1

我在表格的單元格中有UIWebView。我打電話給UIWebView.LoadHtmlString,它在simluator中工作正常,但隨機堆棧跟蹤在設備上崩潰。MonoTouch UIWebView.LoadHtmlString在設備上隨機失敗

at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
    at EP.TapTeam.Application.Main (string[]) [0x00000] in /projects/tapteam/trunk/iOS/EP.TapTeam.UI/Main.cs:17 
    at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff> 

Native stacktrace: 

Thread started: 
0 TapTeam        0x01eb1d79 mono_handle_native_sigsegv + 244 
1 TapTeam        0x01e9e429 mono_sigsegv_signal_handler + 172 
2 libsystem_c.dylib     0x3669472f _sigtramp + 42 
3 UIKit        0x32e452e9 -[UIWebView webView:didCommitLoadForFrame:] + 48 
4 UIKit        0x32e445a7 -[UIWebViewWebViewDelegate webView:didCommitLoadForFrame:] + 22 
5 CoreFoundation      0x3310d7a4 __invoking___ + 68 
6 CoreFoundation      0x3308543d -[NSInvocation invoke] + 108 
7 CoreFoundation      0x330850d9 -[NSInvocation invokeWithTarget:] + 36 
8 WebKit        0x3328f7bd -[_WebSafeForwarder forwardInvocation:] + 408 
9 CoreFoundation      0x3310d68d ___forwarding___ + 576 
10 CoreFoundation      0x33084180 _CF_forwarding_prep_0 + 48 
11 CoreFoundation      0x3310d7a4 __invoking___ + 68 
12 CoreFoundation      0x3308543d -[NSInvocation invoke] + 108 
13 WebCore        0x3210ec3d _ZL11SendMessageP12NSInvocation + 16 
14 WebCore        0x321b1adf _ZL20HandleDelegateSourcePv + 66 
15 CoreFoundation      0x330e1a79 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12 
16 CoreFoundation      0x330e375f __CFRunLoopDoSources0 + 382 
17 CoreFoundation      0x330e44eb __CFRunLoopRun + 230 
18 CoreFoundation      0x33074ec3 CFRunLoopRunSpecific + 230 
19 CoreFoundation      0x33074dcb CFRunLoopRunInMode + 58 
20 GraphicsServices     0x35f3f41f GSEventRunModal + 114 
21 GraphicsServices     0x35f3f4cb GSEventRun + 62 
22 UIKit        0x32c91d69 -[UIApplication _run] + 404 
23 UIKit        0x32c8f807 UIApplicationMain + 670 
24 TapTeam        0x000fe5b8 wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240 
25 TapTeam        0x01de3568 EP_TapTeam_Application_Main_string__ + 152 
26 TapTeam        0x003f3b74 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200 
27 TapTeam        0x01e9fc07 mono_jit_runtime_invoke + 1054 
28 TapTeam        0x01f1a993 mono_runtime_invoke + 90 
29 TapTeam        0x01f1d7eb mono_runtime_exec_main + 306 
30 TapTeam        0x01f1da3f mono_runtime_run_main + 482 
31 TapTeam        0x01ea472b mono_jit_exec + 94 
32 TapTeam        0x01f70b84 main + 2224 
33 TapTeam        0x00018fc0 start + 40 

我發現的唯一的解決方法是調用LoadHtmlString以1秒的延遲:

 NSTimer.CreateScheduledTimer(1f,() => { 
      contentWebView.LoadHtmlString(someInfo, null); 
     }); 

我想知道是否有一個真正的解決方案?

更新,我使用的代碼:

public class OverviewControllerTableSource: UITableViewSource 
{ 

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
{ 
     var cell = tableView.DequeueReusableCell(_cellIdentifier) as OverviewCell_text; 

     if (cell == null) 
     {  
     cell = new OverviewCell_text(_cellIdentifier); 
     } 

    return cell.Bind(_tableItems[indexPath.Section].Items[indexPath.Row]); 
} 

} 

和細胞

public partial class OverviewCell_text : UITableViewCell 
{ 
    public OverviewCell_text (NSString cellId) : base(UITableViewCellStyle.Default, cellId) 
    { 
     NSBundle.MainBundle.LoadNib("OverviewCell_text", this, null); 
      contentWebView.WeakDelegate = new UIWebViewDelegate(); 
      contentWebView.UserInteractionEnabled = false; 
      contentWebView.Layer.CornerRadius = 5f; 
      cellText.ScrollEnabled = false; 
      contentWebView.BackgroundColor = UIColor.Clear; 
    } 

    public UITableViewCell Bind(string someInfo) 
    { 
      // TODO next string without timer randomly crashes on device. 
      if(someInfo != null) 
      { 
       NSTimer.CreateScheduledTimer(1f,() => { 
         contentWebView.LoadHtmlString(someInfo, null); 
       }); 
     } 

      return cell; 
    } 
} 

回答

0

我們需要看到一些代碼來真正幫助你,但因爲你的解決辦法增加了延遲,你在當前請求完成之前,應檢查您是否在UIWebView之內重新輸入。

IWO注意到UIWebViewnot reentrant並且您的額外延遲使得請求完成的可能性更高(但不安全)。

+0

我向帖子中添加了一段代碼。 – 2012-08-09 21:56:33

+0

我沒有看到你的代碼是如何處理重新進入的,即有多個'UIWebView'實例(就像每個單元格一樣)不是一個解決方案。引用(從鏈接到我鏈接到的SO問題的Web博客)「如果您曾嘗試在屏幕上放置多個UIWebView對象並一次將文檔加載到所有對象中,您可能會偶然發現iPhone的無證限制SDK:UIWebView上的loadRequest方法不可重入,「 – poupou 2012-08-10 00:28:02

+0

Poupou,感謝您的幫助。我不確定再入境問題是什麼原因,但在擺脫了Nib之後崩潰消失了,並開始以編程方式創建UIWebView。 – 2012-08-10 20:36:26

0

使UIWebView成爲您的ViewController類(不是內聯變量)的成員用於垃圾收集目的。

+0

我使用Nib來加載單元格,所以UIWebView是類的成員。 – 2012-08-09 21:57:28

+0

作爲一個建議,它通常是一個糟糕的做法,直接在這樣的tableview中放置一個web視圖。我猜你正在碰到poupou提到的某種垃圾回收或重入問題。 – kwcto 2012-08-10 19:20:18