UI在回拨SignalR之前挂起

时间:2016-05-11 05:56:51

标签: ios objective-c signalr signalr-hub

我在这里使用signalR发送数据是我发送数据的代码。

            manager = [AFHTTPRequestOperationManager manager];
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

            setUrl = [NSString stringWithFormat:@"%@%@",[UtilHelper sharedManager].WebServiceURL,@"Save"];
            NSDictionary *parameters = [[NSDictionary alloc]initWithObjectsAndKeys:dicArray,@"products", nil];

            __block NSMutableArray* json;
            json = [[NSMutableArray alloc]init];
            [manager POST:setUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

                NSError *writeError = nil;
                NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&writeError];
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                    options:NSJSONReadingMutableContainers
                                                                      error:&writeError];
                if ([[dic valueForKey:@"StatusMsg"] isEqualToString:@"Success"]) {
                    //  save To S3 Bucket
                    [self sendPicture:[NSString stringWithFormat:@"%@",strProductId] : strPackageId];
                    [[results objectAtIndex:i] setValue:[NSNumber numberWithBool:YES] forKey:@"productsync"];
                    [[results objectAtIndex:i] setValue:[NSNumber numberWithBool:YES] forKey:@"editflag"];
                    [context save:nil];

                }
            }
                  failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                  }];

现在我在AppDelegate

中获得了SignalR响应
[hubConnection setReceived:^(NSString *message) {
    NSLog(@"%@",message);

    });

当setReceived调用我的UI手时,这是我的代码,我呈现我出现的UIViewController并挂起UI init

-(void)handlePlusOption{

    __weak __typeof(&*self)weakSelf = self;

    dispatch_async( dispatch_get_main_queue(), ^{
        __strong __typeof(&*weakSelf)strongSelf = weakSelf;

    strongSelf.addProductVc = [[AddProductVC alloc] initWithNibName:addProductVcIdentifier bundle:nil];
    strongSelf.nav = [[UINavigationController alloc] initWithRootViewController:strongSelf.addProductVc];
    strongSelf.nav.modalPresentationStyle = UIModalPresentationFormSheet;
    strongSelf.nav.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
    strongSelf.nav.modalPresentationCapturesStatusBarAppearance = NO; // To hide status bar, doest work with UIModalPresentationPageSheet style
        [self.navigationController presentViewController:strongSelf.nav animated:YES completion:NULL];
    });
}

为什么用户界面在setReceived之前挂起?

1 个答案:

答案 0 :(得分:0)

  

尝试将setReceived代码块放在此块中..

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // time-consuming task
    [hubConnection setReceived:^(NSString *message) {
         NSLog(@"%@",message);
    }];
    dispatch_async(dispatch_get_main_queue(), ^{
        // run in main thread 
    });

});