如何不冻结UI,并等待响应? [英] How to not freeze the UI, and wait for response?

查看:57
本文介绍了如何不冻结UI,并等待响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从早上开始我一直在尝试,但是我没有达到我想要的.

I've been trying since the morning but I didnt achieve what I wanted.

我尝试了DispatchQueue.main.async和完成块,但是UI中的提交"按钮仍然冻结,等待从服务器返回数据.这是我的代码:

I tried DispatchQueue.main.async and completion block but my "Submit" button in the UI still freezes waiting for the data to be returned from the server. This is my code:

func createData(request:Crudpb_CreateRequest, with completion: @escaping (String) -> Void) throws {
    DispatchQueue.main.async {
        self.response = try! self.client.create(request) // <---- How to handle error for this server call when the server is not available or is down?
        completion(self.response.result)
    }
}

我刚刚注意到我从以下代码中调用了第一个方法,它是一个同步一元,这可能是问题背后的原因.但是再次,我不知道如何在休假状态中调用第二个函数:

I just noticed Im calling the 1st method from the following which is a Synchronous Unary which might be the reason behind the problem. But again I dont know how to call the second function in the fallowing:

 /// Synchronous. Unary.
  internal func create(_ request: Crudpb_CreateRequest, metadata customMetadata: Metadata) throws -> Crudpb_CreateResponse {
    return try Crudpb_CrudServiceCreateCallBase(channel)
      .run(request: request, metadata: customMetadata)
  }
  /// Asynchronous. Unary.
  @discardableResult
  internal func create(_ request: Crudpb_CreateRequest, metadata customMetadata: Metadata, completion: @escaping (Crudpb_CreateResponse?, CallResult) -> Void) throws -> Crudpb_CrudServiceCreateCall {
    return try Crudpb_CrudServiceCreateCallBase(channel)
      .start(request: request, metadata: customMetadata, completion: completion)
  }

服务器端代码:

func (*server) Create(ctx context.Context, req *crudpb.CreateRequest) (*crudpb.CreateResponse, error) {

    var result string
    firstName := req.GetAccount().GetFirstName()
    lastName := req.GetAccount().GetLastName()
    //  id := gocql.TimeUUID()

    fmt.Println("Triggered CREATE function on Go Server " + firstName + " " + lastName + "! Yayy!")

    result = fmt.Sprintf("id for %s %s : %s", firstName, lastName, strconv.Itoa(rand.Intn(100)))
    return &crudpb.CreateResponse{
        Result: result,
    }, nil

当我从服务器获取结果时,我需要此应用程序/提交按钮不冻结.

I need this app / submit button not to freeze while it fetches result from server.

请帮助.

推荐答案

改用异步函数,并调用create函数完成内部的完成块.

Use the asynchronous function instead and call the completion block inside create function's completion.

func createData(request:Crudpb_CreateRequest, with completion: @escaping (String) -> Void) throws { 
try! self.client.create(request) { (response: Crudpb_CreateResponse?, result: CallResult) in 
  DispatchQueue.main.async {
// This is assuming your completion involves UI operations. Otherwise there is no need for this async call.

let stringOutput = String(data: result.resultData!, encoding: String.Encoding.utf8))
    completion(stringOutput)
    }
  }
}

这篇关于如何不冻结UI,并等待响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆