gpt4 book ai didi

ios - 从 ios 应用程序在服务器上上传大型音频时如何检查 block 仍在后台运行

转载 作者:行者123 更新时间:2023-11-29 01:29:54 27 4
gpt4 key购买 nike

我正在使用 afnetworking 多部分数据将音频文件上传到服务器。我为此创建了一项功能,上传进度在后台进行,以便用户可以在上传运行时使用应用程序。函数代码为:

class func postAudioURL(serverlink:String,methodname:String,param:NSDictionary,userName:String,password:String,filepath:String ,CompletionHandler:(success:Bool,response:NSDictionary) -> ())
{
print(serverlink + methodname + " and Param \(param)")

let notallowchar : NSCharacterSet = NSCharacterSet(charactersInString: "01234").invertedSet
let dateStr:String = "\(NSDate())"
let resultStr:String = (dateStr.componentsSeparatedByCharactersInSet(notallowchar) as NSArray).componentsJoinedByString("")
let fileFormatedname = "RecordedAudio" + resultStr + ".wav"

let audiodata : NSData = NSData(contentsOfFile: filepath)!

let manager = AFHTTPRequestOperationManager(baseURL: NSURL(string: serverlink))
manager.requestSerializer.setAuthorizationHeaderFieldWithUsername(AUTH_USERNAME, password: AUTH_PWD)

manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)


manager.POST(methodname, parameters: param, constructingBodyWithBlock: { (formdata:AFMultipartFormData!) -> Void in
formdata.appendPartWithFileData(audiodata, name: "AudioFile", fileName: fileFormatedname, mimeType: "Audio/wav")
}, success: { (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) -> Void in
print("Response : " + responseObject.description)
CompletionHandler(success: true, response: responseObject as! NSDictionary)
}, failure: { (operation: AFHTTPRequestOperation!,
error: NSError!) -> Void in
print("Error: " + error.localizedDescription + "error code : \(error.code)")
var statuscode:String = ""
if(operation != nil) {
print("Response string error : \(operation.responseString) response code : \(operation.response.statusCode)")
statuscode = String("\(operation.response.statusCode)")

}
let errDict:NSDictionary = ["message":"\(error?.localizedDescription)","StatusCode":statuscode]
CompletionHandler(success: false,response: errDict)
})
}

此代码运行良好,没有任何问题。我在这里写了指定如何使用完成 block 上传音频。

我已管理一个本地数据库,以根据此上传功能响应了解文件正在等待上传、发送或失败。大多数时候我会得到成功或失败的响应,我会相应地更新数据库。

问题是,对于某些音频,我无法通过任何随机问题或杀死应用程序来获得成功或失败的响应。因此,该文件统计信息一直处于未决状态。我需要一个解决方案,例如我可以检查是否有某些功能或完成 block 仍在运行?这样我就可以更新,就像如果没有运行一样,我会将所有挂起状态更新为失败并重新上传。

此问题很少见,但我仍然需要解决方案,因为它被视为我的应用程序中的错误。任何帮助将不胜感激。提前致谢。

===编辑

我使用了后台任务过期功能,这样如果应用程序进入后台,音频上传仍然可以运行,并在成功或失败时结束该后台任务。我的这个函数调用就像

func uploadAudioServiceCall(metadata : String , myFileUrl : String) {

self.bgTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
UIApplication.sharedApplication().endBackgroundTask(self.bgTask)
})
let webParam : [String:String] = ["data" : metadata]

WebService.postAudioURL(MAINLINK, methodname: UPLOAD_METHOD, param: webParam, userName: AUTH_USERNAME, password: AUTH_PWD, filepath: myFileUrl) { (success, response) -> () in
if success == true {
//"Response":"Success"
// update state pending to sent
let allPending = //get pending count from database
if allPending.count == 0 {
UIApplication.sharedApplication().endBackgroundTask(self.bgTask)
}
} else {
// update state pending to fail
let allPending = //get pending count from database

if allPending.count == 0 {
UIApplication.sharedApplication().endBackgroundTask(self.bgTask)
}
}
}
}

最佳答案

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

almofire 上传将在这种情况下停止,因此执行 .success 或 .failure 闭包没有变化。您可以在此处将“待处理”状态设置为失败。

关于ios - 从 ios 应用程序在服务器上上传大型音频时如何检查 block 仍在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560186/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com