dispatch_async_f 详解

本文介绍dispatch_async_f函数的使用方法,包括其参数解释及一个简单的Swift示例。该函数用于异步执行任务,并将自定义上下文传递给指定的函数。

之前面试的时候被问过一个问题:你知道dispatch_async_f 吗?我一脸蒙比,问题好变态,太偏了吧,见过但是没用过,不知道干啥的。。今天一时兴起,想了解一下,但是网上一查,没发现一个详细说并且带demo的。。委屈看来要自已动手,丰衣足食,下面是参数的官方文档:

context: The application-defined context parameter to pass to the function.

        work:      The application-defined function to invoke on the target queue. The first parameter passed to this function is the value of the context parameter. This parameter          cannot  be NULL.

        大概意思:work是一个函数指针,context是work的第一个参数(用的时候注意context传的是地址,不要传值)

         work的类型是dispatch_function_t   也就是      typedef void (*dispatch_function_t)(void *_Nullable);

demo用法如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    int context = 10;
    dispatch_async_f(dispatch_get_global_queue(0, 0), &context, test);
}

void test(void*context) {
    int *c = context;
    NSLog(@"%d", *c);
}






esp_err_t esp_http_client_perform(esp_http_client_handle_t client) { esp_err_t err; do { if (client->process_again) { esp_http_client_prepare(client); } switch (client->state) { /* In case of blocking esp_http_client_perform(), the following states will fall through one after the after; in case of non-blocking esp_http_client_perform(), if there is an error condition, like EINPROGRESS or EAGAIN, then the esp_http_client_perform() API will return ESP_ERR_HTTP_EAGAIN error. The user may call esp_http_client_perform API again, and for this reason, we maintain the states */ case HTTP_STATE_INIT: if ((err = esp_http_client_connect(client)) != ESP_OK) { if (client->is_async && err == ESP_ERR_HTTP_CONNECTING) { return ESP_ERR_HTTP_EAGAIN; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } /* falls through */ case HTTP_STATE_CONNECTED: if ((err = esp_http_client_request_send(client, client->post_len)) != ESP_OK) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } /* falls through */ case HTTP_STATE_REQ_COMPLETE_HEADER: if ((err = esp_http_client_send_post_data(client)) != ESP_OK) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } /* falls through */ case HTTP_STATE_REQ_COMPLETE_DATA: /* Disable caching response body, as data should * be handled by application event handler */ client->cache_data_in_fetch_hdr = 0; int64_t ret = esp_http_client_fetch_headers(client); if (ret < 0) { if ((client->is_async && errno == EAGAIN) || ret == -ESP_ERR_HTTP_EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } /* Enable caching after error condition because next * request could be performed using native APIs */ client->cache_data_in_fetch_hdr = 1; if (esp_transport_get_errno(client->transport) == ENOTCONN) { ESP_LOGW(TAG, "Close connection due to FIN received"); esp_http_client_close(client); http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return ESP_ERR_HTTP_CONNECTION_CLOSED; } http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return ESP_ERR_HTTP_FETCH_HEADER; } /* falls through */ case HTTP_STATE_RES_ON_DATA_START: /* Enable caching after fetch headers state because next * request could be performed using native APIs */ client->cache_data_in_fetch_hdr = 1; if ((err = esp_http_check_response(client)) != ESP_OK) { ESP_LOGE(TAG, "Error response"); http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ERROR, &client, sizeof(esp_http_client_handle_t)); return err; } while (client->response->is_chunked && !client->is_chunk_complete) { if (esp_http_client_get_data(client) <= 0) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } ESP_LOGD(TAG, "Read finish or server requests close"); break; } } while (client->response->data_process < client->response->content_length) { if (esp_http_client_get_data(client) <= 0) { if (client->is_async && errno == EAGAIN) { return ESP_ERR_HTTP_EAGAIN; } ESP_LOGD(TAG, "Read finish or server requests close"); break; } } http_dispatch_event(client, HTTP_EVENT_ON_FINISH, NULL, 0); http_dispatch_event_to_event_loop(HTTP_EVENT_ON_FINISH, &client, sizeof(esp_http_client_handle_t)); client->response->buffer->raw_len = 0; if (!http_should_keep_alive(client->parser)) { ESP_LOGD(TAG, "Close connection"); esp_http_client_close(client); } else { if (client->state > HTTP_STATE_CONNECTED) { client->state = HTTP_STATE_CONNECTED; client->first_line_prepared = false; } } break; default: break; } } while (client->process_again); return ESP_OK; }该函数怎么使用
最新发布
11-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值