2017-02-02 1 views
1

여기서 뭐이있어, 데이터로드가 완료되기 전에 중지 점점 데이터json 데이터를 신속하게 가져 오는 동안 활동 표시기를 시작하고 중지하는 방법 2?

let urlstring = "example.com" 
     let session = NSURLSession.sharedSession() 
     let url = NSURL(string: urlstring) 

     session.dataTaskWithURL(url!) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in 

      if let responseData = data 
      { 

       // var names = [String]() 
       do { 
        let json = try NSJSONSerialization.JSONObjectWithData(responseData, options: .AllowFragments) 

        if let job = json["response"] as? [[String: AnyObject]] { 
         if job.count == 0{ 
          print("error") 
         } 
         for jobs in job { 
          if let id = jobs["id"] as? String, let ccode = jobs["city_code"] as? String, let cname = jobs["city_name"] as? String, let cparent = jobs["city_parent"] as? String{ 
           if check_city_exist(id){ 
            insert_cities(id, city_code: ccode, city_name: cname, city_parent: cparent) 
           } 
           else{ 
            update_cities(id, city_code: ccode, city_name: cname, city_parent: cparent) 
           } 
          } 
         } 

        } 
       } catch { 
        print("error serializing JSON: \(error)") 
       } 

      } 


      }.resume() 

내 기능 및 표시 등 표시 및보기가 즉시로드 표시기를 시작하고 때

prog.startAnimating() // prog is my activity indicator 
let backgroundQueue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) 
    dispatch_async(backgroundQueue) {() -> Void in 
     load_data() 
     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      self.prog.stopAnimating() 
     }) 
    } 

을을 stoping에있는 viewDidLoad 내 코드입니다 문제?

답변

0

문제는 loadData이 또 다른 비동기 작업이므로 코드 실행이 다음 줄로 이동하여 즉시 표시기가 멈 춥니 다!

당신은 당신의 데이터로드 폐쇄 내부에 정지 전화를 이동해야

- 당신이 jobs 루프 처리를 완료 한 후에 - 모든 UI에 대한 dispatch_get_main_queue을 포함해야합니다은

+0

도움이되기를 바랍니다. 답변이 유용 할 경우 동의하십시오. – Russell

0

를 호출 내가 그것을 일 희망 수립 하십하시기 바랍니다!

let urlstring = "example.com" 
    let session = NSURLSession.sharedSession() 
    let url = NSURL(string: urlstring) 

    prog.startAnimating() // Here Start Animating 

    session.dataTaskWithURL(url!) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in 

     self.prog.stopAnimating() // here we stop animating because response is either success or failure 

     if let responseData = data 
     { 
      //Here do with response 
     } 


     }.resume() 
    } 
관련 문제