Ios 连续从服务器获取数据

Ios 连续从服务器获取数据,ios,swift2,xcode7,Ios,Swift2,Xcode7,我需要不断地从服务器获取数据。我已经搜索过了,然后我想到了使用NSTimer在特定的时间间隔在后台线程中连续调用服务 var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "callService", userInfo: nil, repeats: true) func callService() { let qualityOfServiceClass

我需要不断地从服务器获取数据。我已经搜索过了,然后我想到了使用NSTimer在特定的时间间隔在后台线程中连续调用服务

    var timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "callService", userInfo: nil, repeats: true)

    func callService()
     {
       let qualityOfServiceClass = QOS_CLASS_BACKGROUND
       let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
        .....

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
          ......
        })
      })
     }
我们能这样做吗?。这是一个好方法吗?还是有其他方法可以做到这一点?我真的很想知道
didUpdateLocation

CLLocationManagerDelegate
中工作。我想实现类似的方法。请给出您的想法。

我是通过socket.io实现的。我看过这个网站。当我浏览那个链接时,我能理解他们和我一样,使用NSTimer在特定的时间间隔调用服务。但它使用了巨大的电池电量。然后他们用插座

我从github获得了swift 2的socket.io代码


它解决了我的问题。

不,你不能在后台使用nstimer。您应该查看后台获取,或者如果您需要更频繁的更新,请推送messagesyeah。我确实研究过推送消息。它将只有256字节,但我的数据可能超过此大小限制。还取决于我需要更新UI的服务器数据(比如设备列表的纬度和经度)。我也学习了背景知识。但我的情况并非如此。在NSTimer场景中,我可以在没有后台线程的单独线程中调用服务吗?我真的需要像“CLLocationManagerDelegate”中的“didUpdateLocation”方法那样的方法。我想从服务器自动连续更新数据。你知道它是如何工作的吗?这不是iOS支持的模式。移动设备不是通用计算机。它有电池和带宽限制。您的选项是推送通知,提示您的应用程序下载一些附加信息或后台获取模式,但这两种方式都有速率限制。好的,谢谢。我知道这必须在服务器中实现。我做任何事情都要做iOS方面的研究。谢谢你的回复。我将学习如何在服务器中实现“didUpdateLocation”。非常感谢。
let socket = SocketIOClient(socketURL: "localhost:8080")

socket.on("connect") {data, ack in
    print("socket connected")
}

socket.on("currentAmount") {data, ack in
    if let cur = data[0] as? Double {
        socket.emitWithAck("canUpdate", cur)(timeoutAfter: 0) {data in
            socket.emit("update", ["amount": cur + 2.50])
        }

        ack?.with("Got your currentAmount", "dude")
    }
}

socket.connect()