我正在分离一个线程在后台执行一些操作,请参考下面的代码
currentThread = [[NSThread alloc]initWithTarget:contactServiceselector:@selector(requestForContactBackup:)object:msisdn];
[currentThread start];
Run Code Online (Sandbox Code Playgroud)
这个currentThread就是AppDelegate中声明的指针。我的视图上有一个按钮,点击它,后台线程的执行应该停止。参考下面的代码:
-(void)cancelTheRunningTasks {
if(self.currentThread !=nil) {
[currentThread cancel];
NSLog(@"IsCancelled: %d",[currentThread isCancelled]); //here Yes returns
[self removeNetworkIndicatorInView:backUpViewController.view];
}
}
Run Code Online (Sandbox Code Playgroud)
下面代码的问题是后台线程仍然处于执行状态。
我的问题是,有了线程引用,如何从主线程取消/停止执行/杀死后台线程?
请建议我可能的解决方案。谢谢。
我正在执行 Post 请求,为此我必须设置标头内容类型,因为application/json我正在编写以下代码:
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
res = curl_easy_setopt(curl, CURLOPT_HEADER,headers);
Run Code Online (Sandbox Code Playgroud)
但是当我编译程序时,错误出现为
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 22
Content-Type: application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
application/json请帮我尽快设置此内容类型