小编Uba*_*ade的帖子

iOS Alamofire超时且无网络连接

即使没有网络连接,我也会收到超时错误。我正在使用Alamofire发送请求。我已经看到了检查网络连接的方法,例如如何在Swift中使用SCNetworkReachability,但据我所读,最好发送请求而不是在每个请求之前检查网络连接。我的问题是,即使没有网络连接,为什么还会引发超时错误?有没有一种方法可以检查两者之间的距离,还是应该在超时的情况下仅检查网络的可达性。

import Foundation
import Alamofire

var config = URLSessionConfiguration.default
var manager: SessionManager? = nil
var configured: Bool = false
let formatter = DateFormatter()

let postURL = URL(string: "http://httpbin.org/post")!

func postStatus(deviceID: String, status: Bool, latitude: Double, longitude: Double){
    print("\nPosting")

    if(!configured){
        print("Configuring")
        config.timeoutIntervalForResource = 4
        config.timeoutIntervalForRequest = 4
        manager = Alamofire.SessionManager(configuration: config)
        configured = true
    }else{
        print("Configured\n")
    }

    let postData: [String: Any] = ["deviceID": deviceID, "status": 0, "lat": String(latitude), "long": String(longitude), "timestamp": getCurrentTime()]

    manager?.request(postURL, method: HTTPMethod.post, parameters: postData, encoding: JSONEncoding.default, …
Run Code Online (Sandbox Code Playgroud)

networking ios swift alamofire

5
推荐指数
1
解决办法
1009
查看次数

在C中分叉 - 无限循环?

试图学习C分叉.它正确打印主循环运行的次数和正确的线程数,但执行时间关闭,程序永远不会终止.我是否制作了无数的流程?

经过一些建议后,这是一个更清洁的代码版本.旧版本位于下方.更新后的部分仍在创建许多子进程,并且永远不会退出.我只是没有看到出了什么问题.

更新:John Hascall的建议修复了格式错误和线程乱序运行.仍然生成无限数量的线程,但现在按正确的顺序.即打印线程执行时间1,2,3,4 ......等.不要认为问题是等待系统调用,而是去研究它,看看我是否找不到任何东西.

更新**:我找到了解决方案.我认为第一个问题是我没有等待命令,第二个问题是在等待时我意外地删除了计数<argv [1]的检查.我把它放回去,似乎运行正常!感谢大家的帮助和风格指针!工作版本如下.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "./processes.h"

int main(int argc, char** argv) {
  if (argc != 4) {
    printf("Wrong number of arguments entered. Usage: #processes sleepTime inputFile.\n");
    return 1;
  }

  if(atoi(argv[1]) <= 0){
    printf("Incorrect number of children, must be greater than 0");
    return -1;
  }

  int count = 0;
  int index;
  Child *child = malloc(sizeof(Child) * atoi(argv[1]));
  int childIndex;
  int pid;

  do{
    switch (pid = fork()){
      case -1:
        printf("Fork failed\n");
        exit(1);

      case …
Run Code Online (Sandbox Code Playgroud)

c fork

4
推荐指数
1
解决办法
1524
查看次数

iOS BLE 的断开时间

我注意到,当您在应用程序中断开蓝牙设备的连接时,iOS 设备将继续保持该连接约 10 秒钟。我试图通过写入一个特性来解决这个问题,该特性导致蓝牙模块取消与 iOS 设备的连接,但这不起作用(主要是因为我更改了模块而 iOS 没有看到更改,因为我假设该设备缓存在某处)。有没有办法让它在代码中立即断开连接?我manager.cancelPeripheralConnection(peripheral)目前正在使用 swift 命令 。

ios bluetooth-lowenergy swift

1
推荐指数
1
解决办法
2133
查看次数

标签 统计

ios ×2

swift ×2

alamofire ×1

bluetooth-lowenergy ×1

c ×1

fork ×1

networking ×1