removeFromSuperview() takes too long

elyrico

I try to save an object from my ViewController.swift with Core Data after picking an image with the imagePickerController. I display a view (DynamicView) with a spinner while saving. The object is saved in 1 or 2 seconds, but the DynamicView takes 7 or 8 seconds to be removed from the superView.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
    dismissViewControllerAnimated(true, completion: nil)
    picture = info[UIImagePickerControllerOriginalImage] as? UIImage
    view.addSubview(DynamicView)
    var newImageData = UIImageJPEGRepresentation(picture, 1)
    objectToSave?.photo = newImageData
    progressBarDisplayer("test", true)
    dispatch_async(dispatch_get_global_queue(
        Int(QOS_CLASS_USER_INTERACTIVE.value), 0)) {
            self.save()
    }
}

func save() {
    var error : NSError?
    if(!managedObjectContext!.save(&error) ) {
        println(error?.localizedDescription)
    }else{
        println("No error, saved")
        self.DynamicView.removeFromSuperview()


    }
    NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
} 
Quentin Hayot

Call removeFromSuperview() from the main thread to make sure that your UI is synchronized:

func save() {
    var error : NSError?
    if(!managedObjectContext!.save(&error) ) {
        println(error?.localizedDescription)
    }else{
        println("No error, saved")
        dispatch_async(dispatch_get_main_queue(),{            
            self.DynamicView.removeFromSuperview()
        }


    }
    NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related