Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completion Handler is not working in viewDidLoad?

I'm using this library in my app for banners. I am trying to get the Link by parsing the JSON.

The Images are are not showing in the slideshow view. If I press the slideshow view, after that everything works fine. I thought that there was some issue with my completion handler. But I can't solve it yet :)

@IBOutlet weak var slideshow: ImageSlideshow!
var transitionDelegate: ZoomAnimatedTransitioningDelegate?
var Banner : [AlamofireSource] = []

override func viewDidLoad() {
        super.viewDidLoad()
        Banners { (imagesource) in
            if imagesource != nil {
                self.bannershow()
            }
        }
  }

    func Banners(completionHandler: ([AlamofireSource]?) -> ()) -> (){
        Alamofire.request(.GET, "http://46.420.116.11/mobileapp/gps/api.php?rquest=get_banners")
            .responseJSON{ response in
                if let data = response.result.value{
                    let json = JSON(data)
                    let count = json["image_path"].count
                    for index in 0...count-1 {
                        let image :String = json["image_path"][index].stringValue
                        let source : AlamofireSource = AlamofireSource(urlString: image)!
                        self.Banner.append(source)
                    }
                    completionHandler(self.Banner)
                }
        }
  }

    func bannershow(){
        self.slideshow.backgroundColor = UIColor.whiteColor()
        self.slideshow.slideshowInterval = 2.0
        self.slideshow.contentScaleMode = UIViewContentMode.ScaleToFill
        self.slideshow.setImageInputs(self.Banner)

        let recognizer = UITapGestureRecognizer(target: self, action: "click")
        self.slideshow.addGestureRecognizer(recognizer)
        }

 func click() {
        let ctr = FullScreenSlideshowViewController()
        ctr.pageSelected = {(page: Int) in
            self.slideshow.setScrollViewPage(page, animated: false)
        }

        ctr.initialPage = slideshow.scrollViewPage
        ctr.inputs = slideshow.images
        self.transitionDelegate = ZoomAnimatedTransitioningDelegate(slideshowView: slideshow);
        ctr.transitioningDelegate = self.transitionDelegate!
        self.presentViewController(ctr, animated: true, completion: nil)
    }
like image 936
user3467240 Avatar asked Feb 13 '16 18:02

user3467240


1 Answers

You probably have a threading problem. There is no guarantee that the Banners completion handler is called on the main thread. You need to step out to the main thread explicitly before doing anything that touches your properties or (especially) the interface.

like image 178
matt Avatar answered Nov 07 '22 17:11

matt