-3
  1. During resistration of a user, i am uploading some photoes to the server. The first one is the profile photo. And the rest photoes i want to upload after the resistration process once completed. Means i want to upload the photo after getting the userid and the access token.Means after a successfully image upload, the user will enter to the "matchesViewController" and then only the upload process will start. Also i want to upload these photoes in background thread. Also if the app rinning in background then also the upload process should be continue. All the uploading process is done by Alamofire.

  2. By writting the following code the photos are uploading successfully, but it is taking more time. Which is not a good user experience.

  3. Also please tell me how to continue the upload process when the app running in back ground thread.

  4. Also if the code is not good please help me to modify any changes in code if needed.

My code is: -

private func handleRegistration (_ parameterDict : [String : Any]){

    let url = USER_REGISTER_URL
    let headers: HTTPHeaders = [
        "Content-Type": "application/x-www-form-urlencoded"

    ]


    Alamofire.upload(multipartFormData: { (multipartFormData) in
        for (key, value) in parameterDict {

            multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
        }
        let random = randomString(length: 7)
        multipartFormData.append(self.selectedProfilePic.image!.jpegData(compressionQuality: 0.4)!, withName: "fileset",fileName: "\(random).jpg", mimeType: "image/jpg")




    }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                print(response)

                var userId: String?
                var token: String?
                if let result = response.result.value {
                    let JSON = result as! NSDictionary



                    guard let accessToken = JSON["access_token"] as? String
                        else{ return }


                    guard let uid = JSON["User_Id"] as? String
                        else{return}
                    userId = uid
                    token = "bearer " + accessToken


                    UserDefaults.standard.set(Int(uid), forKey: "User_Id")
                    UserDefaults.standard.set(token, forKey: "access_token")


                    let phone = JSON["Phone_Number"] as? String
                    let FirstName = JSON["FirstName"] as? String

                    let email = uid + "@gmail.com"
                    let tempDictionary : Dictionary = [kFIRSTNAME : FirstName!, kLASTNAME : "", kFULLNAME : FirstName!, kPHONE : phone!, kREGISTERUSEID: userId, kEMAIL: email] as [String : Any]


                    self.checkDeviceTokenAvailibility(uid: Int(userId!)!)
                    self.startRegistrationWithFirebase( detailDict: tempDictionary)

                    let storyBoard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainTabbarView")
                    storyBoard.modalPresentationStyle = .fullScreen
                    self.present(storyBoard, animated: true, completion: nil)



                    if let message = JSON["Message"] as? String{

                        if message == "Please Upload a image."{

                        }
                    }

                }

                if userId != nil  && token != nil{



                    self.multiImageUpload(userId: Int(userId!)!, token: token!)
                }
                print("Succesfully uploaded")




                if let err = response.error{
                    //                            onError?(err)
                    //                            print(err.localizedDescription)
                    return
                }
                //                        onCompletion?(nil)
            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")
            //                    onError?(error)
        }
    }
}
Ios developer
  • 54
  • 1
  • 6

1 Answers1

-1

One way to make the upload time a lot less is to send multiple parts at once using a for loop and using Alamofire to send a specific part. That way, it breaks it up into parts that take a lot less time to send. The con is that on the server side you will have to reconstruct the image.

Also if the code is not good please help me to modify any changes in code if needed.

It seems you have a lot of arbitrary space in your code.

Sylvan M.
  • 160
  • 7