2

I have a set up for a picture selection as follows:

@IBOutlet weak var imageHolder: UIImageView!
    @IBAction func addImage(_ sender: UIButton) {
        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
        pickerController.allowsEditing = true
        self.present(pickerController, animated: true, completion: nil)    
    }
    var imageSaved = UIImage()

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        self.dismiss(animated: true, completion: nil)
        self.imageHolder.image = image
        imageSaved = image  
    }

Selecting the picker brings up all of the pictures on the simulator, then the user can select them. Once it is selected the view returns to the original view and displays the picture in the imageView. It is not saved, though, until a "Save" button is hit in this view. This all works fine. The save button is merely an action that saves the image file to the DB.

I have the viewDidAppear set up to query the DB and display the image that has been saved as soon as the user moves into that view... this also works well. However, since the viewDidAppear is run multiple times, there becomes an issue when a new image is first selected to replace the old one. The "Save" button deletes the old object and replaces it, so once it has been replaced everything works fine. However, after selecting the image in the picker (and before hitting "Save"... and therefore before that file is in the DB) the view briefly displays the newly selected the image, but the viewDidAppear runs again and sends it back to the image currently in the DB. Its not technically an error, but it is ugly and I would like it to be fixed but I am not sure how.

This is the viewDidAppear code also:

override func viewDidAppear(_ animated: Bool) {
        let query = PFQuery(className: "ProfilePictures")
        query.whereKey("user", equalTo: (PFUser.current()?.objectId!)! as String)
        query.findObjectsInBackground { (objects, error) in
            if let objectss = objects{
                if objectss.count != 0{
                    for object in objectss{
                        let imagePulled = object["ProfilePicture"] as! PFFile
                        imagePulled.getDataInBackground(block: { (data, error) in
                            if let downloadedImage = UIImage(data: data!){
                                self.imageHolder.image = downloadedImage
                            } else{
                                print(error)
                            }
                        })
                    }

                }
            }
            }
    }

Essentially, how do I get the image to only show the newly selected image in that space between selecting it and hitting save?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Runeaway3
  • 1,439
  • 1
  • 17
  • 43

1 Answers1

1

You should move your initial query in viewDidLoad(). That only gets called once, so it won't replace your image each time the view appears.

Otherwise, if, for some reason, you need to have it in view did viewDidAppear(), you could use dispatch_once to make sure your query only runs the first time the view appears.

Rough example of using dispatch_once:

var token: dispatch_once_t = 0
dispatch_once(&token) { () -> Void in
  // your query goes here
}

If you are using Swift 3, please see this other response on how to get dispatch_once like functionality.

Community
  • 1
  • 1
Andrei Filip
  • 1,145
  • 15
  • 33
  • Ok that makes. The only reason I have it in `viewDidAppear` is because it seems to load faster in there than in `viewDidLoad`. Is this the wrong way to go about this (assuming I just fix my specific problem with the `dispatch_once` that you suggested?) – Runeaway3 Dec 13 '16 at 23:47
  • Also, trying to use `dispatch_once_t` gives me an error of "dispatch_once_t is unavailable in swift: Use lazily initialized globals instead" – Runeaway3 Dec 13 '16 at 23:50
  • Please see the updated response. – Andrei Filip Dec 13 '16 at 23:56
  • However, doing the same query in viewDidLoad() should actually display your image sooner than it does in viewDidAppear(). – Andrei Filip Dec 13 '16 at 23:57