Gomobile绑定委托/回调行为

huangapple go评论80阅读模式
英文:

Gomobile bind delegate/callback behaviour

问题

有人知道是否可以使用gomobile bind,在导出到iOS时实现某种委托行为吗?

例如,我有一个处理iOS应用程序的网络请求的Go库,我需要它以异步方式完成,以免挂起应用程序。

解决方案可以是发送一个Objective-C完成块(我认为这不会起作用,因为我找不到将Objective-C代码发送回Go函数的方法),或者实现某种委托,以便应用程序可以知道请求何时完成。我已经尝试了我能想到的一切...有什么想法吗?谢谢!

英文:

Anyone know if it is possible, using gomobile bind, to implement some sort of delegate behaviour when exporting to iOS?

i.e. i have a go library that handles network requests for an iOS app, and i need it to be done asynchronously so that it doesn't hang the app.

Solutions would be to either send a objc completion block (which i don't think will work, since i have found no way to send objc code back into the go function) or implement some sort of delegate so that the app can know when the request has finished. I have tried everything I could think of... Any ideas? Thanks!

答案1

得分: 2

原来是可以的!

以下是Go代码的翻译:

type NetworkingClient struct {}

func CreateNetworkingClient() *NetworkingClient {
    return &NetworkingClient {}
}

type Callback interface {
    SendResult(json string)
}

func (client NetworkingClient) RequestJson (countryCode string, callback Callback) {
    go func () {
        safeCountryCode := url.QueryEscape(countryCode)
        url := fmt.Sprintf("someApi/%s", safeCountryCode)

        req, err := http.NewRequest("GET", url, nil)
        if err != nil {
            //处理错误
        }

        httpClient := &http.Client{}

        resp, err := httpClient.Do(req)
        if err != nil {
            //处理错误
        }

        defer resp.Body.Close()
        b, err := ioutil.ReadAll(resp.Body)
        callback.SendResult(string(b))
    }()
}

以下是Objective-C中的实现:

- (void)start {

    ...

    EndpointNetworkingClient* client = EndpointCreateNetworkingClient();
    [client requestJson:countryCode callback:self];
}

//从Go接收json字符串
- (void)sendResult:(NSString*)json{
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    id jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    [self handleResponse:jsonDictionary];
}

希望对你有帮助!

英文:

Turns out it is possible!

Here is the Go Code:

type NetworkingClient struct {}

func CreateNetworkingClient() *NetworkingClient {
	return &NetworkingClient {}
}

type Callback interface {
	SendResult(json string)
}

func (client NetworkingClient) RequestJson (countryCode string, callback Callback) {
	go func () {
	safeCountryCode := url.QueryEscape(countryCode)
  	url := fmt.Sprintf("someApi/%s", safeCountryCode)

  	req, err := http.NewRequest("GET", url, nil)
  	if err != nil {
  		//Handle error
  	}

  	httpClient := &http.Client{}

  	resp, err := httpClient.Do(req)
  	if err != nil {
  		//Handle error
  	}

  	defer resp.Body.Close()
	  b, err := ioutil.ReadAll(resp.Body)
      callback.SendResult(string(b))
		}()
  }

Which is implemented as follow in Objetive-C:

- (void)start {

	...

	EndpointNetworkingClient* client = EndpointCreateNetworkingClient();
    [client requestJson:countryCode callback:self];
}

//Receives the json string from Go.
- (void)sendResult:(NSString*)json{
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    id jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    [self handleResponse:jsonDictionary];
}

huangapple
  • 本文由 发表于 2017年1月26日 00:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/41856287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定