4

In my iPhone app I'm using a 3rd party library (libPusher) for WebSockets networking and this library causes every UIScrollView component in my app to become unresponsive. This includes UIScrollViews and UITableView.

What happens is that if a user scrolls one of the UIScrollView components with his finger and happens to keep touching and sliding the view with his finger at the same time that a network operation is under way, then this leads to a completely unresponsive UIScrollView that stops accepting touch events (it thinks that it's in a drag mode all the time even when the finger is lift) and does not decelerate appropriately. The only way out is to destroy the UIScrollView and recreate a new one.

I have contacted the developer of the library but unfortunately so far haven't heard back.

From what I read, this is a common problem when running a run-loop in an un-appropriate mode such as NSDefaultRunLoopMode, however this library seems to be doing the right thing and it runs its run loop in NSRunLoopCommonModes so I'm unclear as what the right solution is.

I tried playing with different modes (tried NSDefaultRunLoopMode) but the behavior is the same.

I'm using iOS 5 and it's the same behavior on the simulator as well as on devices.

Let me paste the code which I think is problematic in the lib and hopefully that'd be enough scope to let you help me find a solution.

In a subclass of NSOperation we have:

- (void)start 
{
  NSAssert(URLRequest, @"Cannot start URLRequestOperation without a NSURLRequest.");

  [self setExecuting:YES];

  URLConnection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self startImmediately:NO];

  if (URLConnection == nil) {
    [self setFinished:YES]; 
  }

  // Common modes instead of default so it won't stall uiscrollview scrolling
  [URLConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  [URLConnection start];

  do {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  } while (!_isFinished);
}

This operation runs on the main thread, as in [[NSOperationQueue mainQueue] addOperation:authOperation];. (maybe this is the problem, but I tried running it in another thread and it crashes so the library will need more work to make it background-thread safe so I can't prove yet that this is the solution...)

So far I tried

  • changing the run loop mode to NSDefaultRunLoopMode - didn't help.
  • running the operation in a new operation queue that I created (e.g. not on the main thread) but the library doesn't seem to be ready for this as it crashes.

I still feel like I'm shooting in the dark... help :)

thanks!

Ran
  • 7,541
  • 12
  • 59
  • 72
  • What this library is neccessary for? You could use the standard Foundation types. If not, try creating a different thread for the network operations using pthreads or NSThread. –  Dec 22 '11 at 21:15
  • The library is a client for the websockets service pusher.com. I'd like to fix the library, but not write a new one from scratch – Ran Dec 22 '11 at 22:15
  • This may be a stupid question, but since you're running on the main thread and do run-loop based networking, why are you using an `NSOperation` in the first place? – danyowdee Dec 26 '11 at 15:12
  • it's not my library so I don't know what the author chose to do this in a run loop, could be backwards compatibility or other requirements driven by this module's dependencies. If I wrote it afresh (which I will probably have to...) then I'd use NSOperation – Ran Dec 26 '11 at 15:39

2 Answers2

2

It's becoming unresponsive since it's doing a while loop on the main thread. This a bad design pattern.

Since you're on iOS5, why not use NSURLConnection's +sendAsynchronousRequest:queue:completionHandler: ?

If you need to be compatible for iOS4.x I would strip out the run loop stuff and take a look at this blog post.

Basically, I would do something like this:

- (void)start
{
    if (![NSThread isMainThread]) {
        return [self performSelectorOnMainThread:@selector(start) 
                                      withObject:nil
                                   waitUntilDone:NO];
    }

    [self willChangeValueForKey:@"isExecuting"];
    isExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];

    NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request 
                                                                   delegate:self];

    self.connection = aConnection;
    [aConnection release];

    if (connection == nil) {
        // finish up here
    }
}
hwaxxer
  • 3,363
  • 1
  • 22
  • 39
  • To generalize this (for example making a macro, kickToMainThread) you can replace `@selector(start)` with `_cmd` but note it won't pass any arugments – Albert Renshaw Oct 28 '17 at 23:01
0

Actually you should kick off NSURLConnection in a singleton thread like this:

+ (void) __attribute__((noreturn)) networkEntry:(id)__unused object
{
    do {
        @autoreleasepool
        {
            [[NSRunLoop currentRunLoop] run];
            NSLog(@"exit worker thread runloop");
        }
    } while (YES);
}

+ (NSThread *)networkThread
{
    static NSThread *_networkThread = nil;
    static dispatch_once_t oncePredicate;

    dispatch_once(&oncePredicate, ^{
        _networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkEntry:) object:nil];
        [_networkThread start];
    });

    return _networkThread;
}

- (void)start
{
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:self.URL
                                                            cachePolicy:NSURLCacheStorageNotAllowed
                                                        timeoutInterval:self.timeoutInterval];
    [request setHTTPMethod: @"GET"];

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

An example is on github. Another good example is AFNetworking, it use the same method.

hrchen
  • 1,223
  • 13
  • 17