removeFromSuperview()需要太长时间

时间:2015-08-19 12:16:16

标签: ios iphone swift core-data

在使用imagePickerController选择图像后,我尝试使用Core Data从ViewController.swift中保存对象。我在保存时使用微调器显示视图(DynamicView)。对象在1或2秒内保存,但DynamicView需要7或8秒才能从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)
} 

1 个答案:

答案 0 :(得分:2)

从主线程调用removeFromSuperview()以确保您的UI已同步:

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