0
此刻,有一个类以JSON
格式(文本参数和0到10 NSNumber
格式参数)向服务器发送1到11个参数。而且类的设计使得当格式改变或删除服务器上已存在的所有数据都被保存并且是新的时,也就是完全重写。这并不是非常令人满意,所以最大数据量只有2.7千字节。任务读取“组织对图像的引用数组”,并且您只需要在当前类中添加一组图像。但是有几个问题:1.如果服务器已经有一个图像,并且我们只是再添加一个图像,结果证明已经重写了这两个图像,如果它们是10?这对用户流量造成严重打击,并且不能纠正服务器与应用程序设计的通信。 2重写超过5张照片时,出现服务器不可用的错误。我想将特定图像发送到服务器上现有的图像阵列,并且每次都不会覆盖它们。只需分配给每个图像ID,就可以从服务器应用程序中删除图像。我尝试了很多不同的选择,但无法达到预期的效果。这里是目前的课程代码如何将图像添加到服务器上的现有图像阵列? Xcode
public class AnswersDataServerEntity: DataSetEntity {
static let DataSetName = "DataSetName"
var id: String? = "0"
var streetId: UInt = 0
var streetName: String? = ""
var answers: [AnswerServerEntity]? = []
var documents: [DocumentServerEntity]? = []
private func save()
{
let deleteCommand = DataSetCommand(dataSetAction: DeleteDataSetAction<AnswersDataServerEntity>(dataSetName: AnswersDataServerEntity.DataSetName, data: self)
deleteCommand.executeWithSuccess(
{ (commond) -> Void in
self.add()
},
errorHandler: { (error) -> Void in
})
}
private func add()
{
let command = DataSetCommand(dataSetAction: AddDataSetAction<AnswersDataServerEntity>(dataSetName: AnswersDataServerEntity.DataSetName, data: self)
command.executeWithSuccess(
{ (command) -> Void in
if let _command = command as? DataSetCommand<AnswersDataServerEntity> {
NSLog("\(_command.dataSetAction)")
}
},
errorHandler: { (error) -> Void in
})
}
static func saveSelected()
{
self.selected().save()
}
static private func selected() -> AnswersDataServerEntity
{
var result = AnswersDataServerEntity()
if let selectedCountry = CountryEntity.selected(){
for answer in AnswerEntity.userAnswers() {
result.answers?.append(AnswerServerEntity(questionId: answer.questionId))
}
}
for document in DocumentEntity.deserializeDocuments() {
let documentImage = UIImage(contentsOfFile: document.documentImageURL)
if let _documentImage = documentImage {
let imageData = UIImageJPEGRepresentation(_documentImage, 1)
if let _imageData = imageData {
let imageString = _imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
result.documents?.append(DocumentServerEntity(documentId: document.documentId, documentTitle: document.documentTitle, documentImageString: imageString))
}
}
}
return result
}
}
public class AnswerServerEntity: DataSetEntity {
var questionId: Int = 0
convenience init (questionId: Int){
self.init()
self.questionId = questionId
}
}
public class DocumentServerEntity: DataSetEntity {
var documentId: Int = 0
var documentTitle: String = ""
var documentImageString: String! // this string will to need to decode in base64string that we can to send it as JSON
convenience init(documentId: Int, documentTitle: String, documentImageString: String) {
self.init()
self.documentId = documentId
self.documentTitle = documentTitle
self.documentImageString = documentImageString
}
}
从服务器的结果,以更好地了解什么去。
request: {
"object" : "storage",
"section" : "api",
"method" : "addData",
"data" : [
{
"answers" : [
{
"questionId" : 201
},
{
"questionId" : 203
},
{
"questionId" : 206
},
{
"questionId" : 210
}
],
"documents" : [
{
"documentImageString" : "\/var\/mobile\/Containers\/Data\/Application\/DD6F8D10-1241-4119-9639-AB983B27CFA6\/Documents\/04DDA484-F257-43BC-A459-3BC7C6050D6F",
"documentTitle" : "xcode",
"documentId" : 0
}
],
"streetName" : "Example",
"id" : "0",
"streetId" : 14
}
],
"token" : "**********************$$$$*$*$*$",
"dataSet" : "DataSetName"
}