0

I am fetching a request from server and trying to display it in tablview. In this process I am want to hide the spinner after fetching records. Problem is : Anything associated with self inside block does not work.

@IBOutlet weak var spinner: UIActivityIndicatorView!
@IBOutlet weak var customTableview: CustomTableView!
var widgetArray :NSMutableArray = []
override func viewDidLoad() {
    super.viewDidLoad()

    print(DataObjects.sharedInstance.mainArray)
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

     spinner.hidden = false
    spinner.startAnimating()

    CustomNetworkHit.networkHitForUrl("http://winjitwinds.cloudapp.net/windapi/windapiservice.svc/getcategorydatapagewise?categoryid=1944&pageno=1", completion: {(arrayResult) -> Void in

        // spinner does not stop animating and it does not hide

        self.spinner.stopAnimating()
        self.spinner.hidden = true
        print("Disable the spinner man")
        DataObjects.sharedInstance.mainArray.addObjectsFromArray(arrayResult as [AnyObject])
        self.widgetArray.addObjectsFromArray(arrayResult as [AnyObject])
        self.customTableview.setUpTableView(self.widgetArray)

        // UItableview Does not reload Data.

        self.customTableview.reloadData()


    })
Tushar Limaye
  • 106
  • 2
  • 7
  • possible duplicate of [Swift UITableView reloadData in a closure](http://stackoverflow.com/questions/26277371/swift-uitableview-reloaddata-in-a-closure) – Dharmesh Kheni Jul 08 '15 at 10:08

1 Answers1

1

Please specify what you mean by "does not work". Without that it's hard to know for sure but I would guess you are trying to do UI work on a background thread (blocks don't run on the main thread).

Within your block try this:

dispatch_async(dispatch_get_main_queue(), ^(){
    // UI CODE GOES HERE
});

In Swift:

dispatch_async(dispatch_get_main_queue()) {
   // UI CODE GOES HERE
}
  • does not work means..using self.spinner.hidden = true does not hide the spinner. I think it is the issue of weakself but still not getting how to declare one in swift. – Tushar Limaye Jul 08 '15 at 10:09
  • UIKit is not thread safe and blocks are not run on the main thread. If you put your UI code in the sample I provided it will work. Weak self just prevents retain cycles. –  Jul 08 '15 at 10:10
  • Thanks man. But it always worked in objective- C without taking it on main thread. – Tushar Limaye Jul 08 '15 at 10:12