【问题标题】:UI hang before Call back of SignalR在 SignalR 回调之前 UI 挂起
【发布时间】:2016-05-11 05:56:50
【问题描述】:

我在这里使用 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];
    });
}

为什么 UI 在 setReceived 之前挂起?

【问题讨论】:

    标签: ios objective-c signalr signalr-hub


    【解决方案1】:

    尝试将您的 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 
        });
    
    });
    

    【讨论】:

    • 我已经尝试过了,我的 UI 在调用 setReceived 之前就挂了。
    • 尝试将 setReceived 块放在下面的主线程中..这可能有效,或者如果您正在发送 json 数据,您可以使用另一种方式使用 sbjson 解析器库向您发送数据。
    • 不,它还是一样的。我得到了键值作为响应,我的数据并没有那么大,只能挂起 UI,它只有 6 个键值对。这可能是因为服务器端发送数据吗?
    • @bhadresh 我没听懂你......只是你想将 json 数据发送到只有 6 个键值对的服务器?
    • 好的,用下面的链接改变你的方法..stackoverflow.com/a/37110874/4003548
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多