1

I'm using GCDWebServer to share pictures from my iPhone to others devices on local network.

I'm trying to call the GCDWebServerCompletionBlock inside my own block but my app crashes without any logs but "signal SIGABRT" on GCDWebServerConnection:

GWS_DCHECK(_responseMessage == NULL);

However, if i return directly a response outside my block, all works fine.

Here is my code:

    -(void)handleMediaRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completionBlock {
//...
URLParser *parser = [[URLParser alloc] initWithURLString:url];
      NSString *photoId = [parser valueForVariable:@"id"];

      [[PhotoLibraryManager photoLibraryManager] getPhotoWithId:photoId completionBlock:^(UIImage *image) {

        NSData *data = UIImageJPEGRepresentation(image, 0.70);
        completionBlock([GCDWebServerDataResponse responseWithData:data contentType:@"application/octet-stream"]);

      } failureBlock:^(NSError *error) {
        completionBlock([GCDWebServerDataResponse responseWithStatusCode:404]);
      }];
}

How to call GCDWebServerCompletionBlock inside my own block?

tezqa
  • 139
  • 8

2 Answers2

0

Your are using a synchronous handler so you must return a response from it when the function returns. Not doing so is a programmer error and that's why your app aborts.

It looks like what you want is an asynchronous handler which allows you return the response at a later time. See the "Asynchronous HTTP Responses" section of the GCDWebServer README for more information and examples.

Pol
  • 3,848
  • 1
  • 38
  • 55
  • No that's wrong, i'm using an async handler. And that's why there is a completion block in the method signature. I'm using at a top level: [server addDefaultHandlerForMethod:@"GET" requestClass:[GCDWebServerRequest class] asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) { __strong typeof(weakSelf)strongSelf = weakSelf; [strongSelf handleMediaRequest:request completion:completionBlock]; }]; – tezqa Oct 30 '16 at 09:33
  • My bad, I read your code too fast. Can you add the stack trace leading to `GWS_DCHECK(_responseMessage == NULL);` in your question? – Pol Nov 01 '16 at 19:47
0

The crash happens because your are building GCDWebServer in Debug mode and it reports an internal inconsistency. You are most likely calling completionBlock() more than once, which is not allowed. It must be called once and exactly once for each response for each request.

Pol
  • 3,848
  • 1
  • 38
  • 55