简体   繁体   中英

removeFromSuperview() takes too long

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

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

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