MonoTouch UIWebView.LoadHtmlString在设备上随机失败

时间:2012-08-09 17:52:20

标签: ios uiwebview xamarin.ios

我在表格的单元格中有UIWebView。我正在调用UIWebView.LoadHtmlString并且它在simluator中工作正常,但在具有以下堆栈跟踪的设备上随机崩溃。

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

我找到的唯一解决方法是以1秒的延迟调用LoadHtmlString:

        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]);
}

}

和Cell

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;
    }
}

2 个答案:

答案 0 :(得分:0)

我们需要看一些代码才能真正帮助您,但是,由于您的解决方法会增加延迟,因此您应检查在当前请求完成之前您是否未在UIWebView内重新输入。

IWO请注意UIWebViewnot reentrant,您的额外延迟会使请求更有可能(但不安全)。

答案 1 :(得分:0)

使UIWebView成为ViewController类的成员(不是内联变量)以进行垃圾收集。