我正在设计一个聊天应用程序,我已经为用户设置了以下机制来上传消息。基本上,我将消息推送到队列中并一个接一个地上传它们。当队列为空时,我调用finishedUploading
每秒运行一次,如果队列中有任何内容,则重新运行任务。
var uploadQueue:[UploadMessage]?
let session = NSURLSession.sharedSession()
let lockQueue = dispatch_queue_create("com.dsdevelop.lockQueue", nil)
// RETURNS AMOUNT OF ITEMS STILL IN QUEUE
func getRemainingActiveUploads() -> Int {
return (self.uploadQueue != nil) ? self.uploadQueue!.count : 0
}
//REMOVES MESSAGE FROM QUEUE ONCE UPLOADED
func removeMessageFromUploadQueue(messageToBeRemoved : UploadMessage) {
if (uploadQueue != nil) {
dispatch_sync(lockQueue) {
self.uploadQueue = self.uploadQueue?.filter({$0.date!.compare(messageToBeRemoved.date!) == NSComparisonResult.OrderedSame})
}
}
}
var uploadTimer : NSTimer?
// CALLED ONLY WHEN UPLOADQUEUE IS EMPTY, RERUNS THE UPLOAD FUNCTION AFTER 1 SECOND
func finishedUploading() {
uploadTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(uploadAllLinks), userInfo: nil, repeats: false)
if (needToRefetch) {
needToRefetch = false
newMessageReceived()
}
}
func uploadAllLinks()
{
uploadTimer?.invalidate()
uploadTimer = nil
// suspending queue so they don't all finish before we can show it
session.delegateQueue.suspended = true
session.delegateQueue.maxConcurrentOperationCount = 1
let myUrl = NSURL(string: "http://****")
// create tasks
if (uploadQueue != nil) {
if (uploadQueue?.count > 0) {
for message in uploadQueue!
{
let request = NSMutableURLRequest(URL:myUrl!)
request.HTTPMethod = "POST"
request.timeoutInterval = 10
request.HTTPShouldHandleCookies=false
var postString = "sender=" + message.sender!
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let dltask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
if data != nil
{
do {
let jsonArray = try NSJSONSerialization.JSONObjectWithData(data_fixed!, options:[])
dispatch_async(dispatch_get_main_queue(), {
if let errorToken = jsonArray["error"] as! Bool? {
if !errorToken {
self.uploadQueue = self.uploadQueue!.filter({$0.date!.compare(message.date!) != NSComparisonResult.OrderedSame})
let remaining = self.getRemainingActiveUploads()
print("Downloaded. Remaining: \(remaining)")
if (remaining == 0) {
self.finishedUploading()
}
}
else {
let remaining = self.getRemainingActiveUploads()
print("Downloaded. Remaining: \(remaining)")
if (remaining == 0) {
self.finishedUploading()
}
}
}
else {
let remaining = self.getRemainingActiveUploads()
print("Downloaded. Remaining: \(remaining)")
if (remaining == 0) {
self.finishedUploading()
}
}
})
}
catch {
print("Error: \(error)")
}
}
})
print("Queuing task \(dltask)")
dltask.resume()
}
session.delegateQueue.suspended = false
}
else {
finishedUploading()
}
// resuming queue so all tasks run
}
}
现在这在以下两种情况下可以正常工作:
finishedUploading
并每秒运行uploadAllLinks
以检查uploadQueue
remaining == 0
因此finishedUploading
被称为但是,每当队列有多个项目时,第一个项目上传,if remaining == 0
失败,然后没有任何反应。我不明白为什么此时队列中的其他项目没有运行for循环。
答案 0 :(得分:1)
我怀疑问题是你的10秒超时间隔。一旦数据任务创建,它就会开始计时,如果任务保持空闲(没有接收新数据)超过十秒,则终止任务。
如果您有多个任务且操作系统一次只允许上传其中一个或两个任务,那么排队等待启动的任何任务将永远不会完成。我不认为文档提到了这一点。
实际上,这种设计使NSURLSession的排队不太理想,因此,大多数人似乎编写自己的队列并自己处理并发限制,确保每个任务都在它之前创建应该开始运行。我建议做类似的事情:
此外,10秒对于超时间隔来说太短了,除非您的设备安装在墙上并且在Wi-Fi上并且保证有固定信号。片状Wi-Fi和弱蜂窝信号可能导致严重的延迟,因此IIRC默认为120秒,尽管我已经在各个地方读取了60。无论哪种方式,你不想要使用10秒。如此短暂的超时几乎可以保证您的应用程序绝对不可靠。