好的,所以我已经成功实现了背景提取。没问题。我遇到的问题是后台提取在模拟器和设备上的工作方式不同。
class func doInBackground(completion: () -> Void) {
let time = NSDate()
print(time)
serverPostRequest()
completion()
}
class func serverPostRequest() {
if(loopCount < 1) {
let params = self.formEncodingBuilder()!
let syncUrl: String = Constants.BASE_SERVER_URL + Constants.SYNC_URL
do {
let opt = try HTTP.POST(syncUrl, parameters: params)
opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
//also notify app of failure as needed
SYNC_STATE = 1
SYNCING = false
}else{
let json = JSON(data: response.data)
if let success = json["synced"].bool {//synced, all done nothing to do anymore
SYNC_STATE = 2
SYNCING = false
}else {//process data
SyncServiceDataSource.dBOperations(json["data"])
print("after")
serverPostRequest()
}
}
}
} catch let error {
print("got an error creating the request: \(error)")
}
}else {
print("Logical error state, check server logs")
SYNC_STATE = 1
SYNCING = false
}
loopCount++
}
class SyncServiceDataSource : DataSourceProtocol {
static func dBOperations(data: JSON) {
print("count")
print(data.count)
}
}
现在背景提取的输出很好,这是
2016-04-20 18:47:12 +0000 计数 19 后 逻辑错误状态,检查服务器日志
但是当我在iphone 5(实际)设备上运行相同的东西时,它只打印
之后什么都没有2016-04-20 18:47:12 +0000
2016-04-20 18:49:12 +0000
之后
SyncServiceDataSource.dBOperations(json["data"])
使用调试器运行或中断
p.s响应数据相对较大json
所以问题是我错过了什么?为什么没有调用SyncServiceDataSource.dBOperations