【问题标题】:NSURLRequest won't fire while UIScrollView is scrollingNSURLRequest 在 UIScrollView 滚动时不会触发
【发布时间】:2011-05-04 16:38:51
【问题描述】:

我有一个问题,我试图在用户在 UIScrollView 周围移动时在后台加载声音文件...问题是我正在使用 NSURLRequest 所以我可以在后台加载,但即使那样它也拒绝实际加载直到 UIScrollView 停止滚动。 :(

对此我有什么办法吗?

谢谢!

【问题讨论】:

    标签: objective-c ipad ios uiscrollview nsurlrequest


    【解决方案1】:

    NSURLRequest 只管理请求,而不是实际连接。

    滚动等触摸事件会将运行循环放入NSEventTrackingRunLoopMode。默认情况下,NSURLConnection 计划NSDefaultRunLoopMode 中执行。所以在NSEventTrackingRunLoopMode 中,NSDefaultRunLoopMode 被屏蔽了。

    好消息是您可以为NSURLConnection 安排其他模式,例如NSRunLoopCommonModes

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    [connection start];
    

    【讨论】:

    • 谢谢你...让我头疼不已! :-) NSEventTrackingRunLoopMode 和 NSRunLoopCommonModes 有什么区别?
    • AFAIK,在 NSRunLoopCommonModes 中调度的连接将被所有运行循环监视。如果它被安排在 NSEventTrackingRunLoopMode 中,那么它只会在有触摸事件时被监控。 developer.apple.com/library/ios/documentation/cocoa/reference/…
    【解决方案2】:

    我发现如果你调用 startImmediately:YES 或省略这个参数第二行是完全没用的。所以一定要遵循@tidwall 提供的确切模式。

    这也是一个快速的例子:

    self.connection = NSURLConnection(request: self.request, delegate: self, startImmediately:false)
    self.connection?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
    self.connection?.start()
    

    【讨论】:

      猜你喜欢
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 2011-09-15
      • 2013-10-15
      相关资源
      最近更新 更多