2012-08-09 108 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