应用程序一段时间后出现错误 3840 "JSON text did not start with array or object and option to allow fragments not set."
Having Error 3840 "JSON text did not start with array or object and option to allow fragments not set." after a while on the application
I'm having the error 3840 "JSON text did not start with array or
object and option to allow fragments not set." on my application after
being on the same page for a while.
我正在从我的 Parse 服务器调用云代码函数,获取答案并显示它。它运行良好,但稍等片刻后应用程序崩溃了。
我正在给我的经理打电话要求云代码功能:
ParseManager.sharedManager().fetchPrice(startDate: startDate, endDate: endDate, housingId: housing.objectId!) { (objects, error) in
if (objects != []) {
var price = 0
for object in objects! {
price += object["prix"] as! Int
}
price /= objects!.count
self.priceByNight.text = "Price: \(price)"
}
}
在我的经理中:
func fetchPrice(startDate: Date, endDate: Date, housingId: String, completion: PFResults?) {
let params = [
"startDate": startDate,
"endDate": endDate,
"housing": housingId
] as [String : Any]
PFCloud.callFunction(inBackground: "fetchPrice", withParameters: params) { (objects, error) in
if let error = error {
print(error)
completion?(nil, error)
return
}
if let objects = objects as? [PFObject] {
completion?(objects, nil)
}
}
}
这是我的函数发回给我的:
{
"result": [{
"price": 10,
"createdAt": "2019-05-07T12:39:47.320Z",
"updatedAt": "2019-05-09T15:31:25.957Z",
"date": {
"__type": "Date",
"iso": "2019-05-20T12:00:00.000Z"
},
"idHousing": {
"__type": "Relation",
"className": "myOtherClassName"
},
"objectId": "XXXXXXXXXX",
"__type": "Object",
"className": "MyClassName"
}]
}
问题是我如何检查管理器返回的内容,检查方式错误导致它尝试使用空对象执行操作。
解决方案是将 if (objects != []) {
更改为 if let objs = objects {
,现在可以使用了。
I'm having the error 3840 "JSON text did not start with array or object and option to allow fragments not set." on my application after being on the same page for a while.
我正在从我的 Parse 服务器调用云代码函数,获取答案并显示它。它运行良好,但稍等片刻后应用程序崩溃了。
我正在给我的经理打电话要求云代码功能:
ParseManager.sharedManager().fetchPrice(startDate: startDate, endDate: endDate, housingId: housing.objectId!) { (objects, error) in
if (objects != []) {
var price = 0
for object in objects! {
price += object["prix"] as! Int
}
price /= objects!.count
self.priceByNight.text = "Price: \(price)"
}
}
在我的经理中:
func fetchPrice(startDate: Date, endDate: Date, housingId: String, completion: PFResults?) {
let params = [
"startDate": startDate,
"endDate": endDate,
"housing": housingId
] as [String : Any]
PFCloud.callFunction(inBackground: "fetchPrice", withParameters: params) { (objects, error) in
if let error = error {
print(error)
completion?(nil, error)
return
}
if let objects = objects as? [PFObject] {
completion?(objects, nil)
}
}
}
这是我的函数发回给我的:
{
"result": [{
"price": 10,
"createdAt": "2019-05-07T12:39:47.320Z",
"updatedAt": "2019-05-09T15:31:25.957Z",
"date": {
"__type": "Date",
"iso": "2019-05-20T12:00:00.000Z"
},
"idHousing": {
"__type": "Relation",
"className": "myOtherClassName"
},
"objectId": "XXXXXXXXXX",
"__type": "Object",
"className": "MyClassName"
}]
}
问题是我如何检查管理器返回的内容,检查方式错误导致它尝试使用空对象执行操作。
解决方案是将 if (objects != []) {
更改为 if let objs = objects {
,现在可以使用了。