简体   繁体   中英

How to add an image to an existing array of images on the server? Xcode

At the moment, there is a class that sends 1 to 11 parameters to the server in JSON format (a text parameter, and 0 to 10 NSNumber format parameters). And class is designed so that when a change of format or deleted all the data that is already on the server are saved and new, that is, there is a complete rewriting. It's not terribly so that the maximum amount of data just 2.7 kilobytes. Quest reads "To organize an array of references to the image," and that you just need to add an array of images in the current class. But there are several problems: 1. If the server has already one image, and we simply add one more, it turns out there is already rewriting the two images, and if they will be 10? That is a severe blow to user traffic, plus it does not correct the server to communicate with the application design. 2 During rewriting more than 5 pictures, get an error that the server is not available. I want to send a specific image to the existing array of images on the server, and does not overwrite them every time. Just assign to each image ID, it would be possible to delete images from the server application. I tried a lot of different options, but could not achieve the desired result. Here are the current class code

 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
    }
}

The result from the server to get a better understanding of what goes.

  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"
}

I use api of company for sending of images to the server

  static func save(document: DocumentEntity) {
        let image = UIImage(contentsOfFile: document.documentImageURL)
        if let _image = image {
            let uploadCommand = UploadCommand(image: _image)
            uploadCommand.executeWithSuccess({ (success) -> Void in
                if let _success = success as? UploadCommand {
                    if let _outFileName = _success.outFileName {
                        var documentArray = upload()
                        documentArray.append(DocumentPreServerEntity(documentId: document.documentId, documentTitle: document.documentTitle, documentImageURL: _outFileName))
                        let documentsData = NSKeyedArchiver.archivedDataWithRootObject(documentArray)
                        NSUserDefaults.standardUserDefaults().setObject(documentsData, forKey: DocumentPreServerEntity.documentServerEntity)
                        NSUserDefaults.standardUserDefaults().synchronize()
                        AnswersDataServerEntity.saveSelected()
                    }
                }
                }, errorHandler: { (error) -> Void in
                    print(error.localizedDescription)
            })
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM