2017-03-16 9 views
1

私はAlamofireを使用してデータをダウンロードし、JSONで解析しています。私はデータが利用可能であることを発表するために、私たちは閉鎖を使用し、NotificationCenterは使用しないことを知っています。私は閉鎖を理解していない。要求が完了したら、クロージャを使用してテーブルビューを再ロードするにはどうすればよいですか? ここにコードがあります。ここでクロージャを使用して非同期要求が完了したときを知る方法?

func downloadEvents() { 
    let coreDataObject = CoreDataMethods() 
    Alamofire.request(URL(string:URL)!) 
     .responseJSON { response in 
      switch response.result { 
      case .success: 
       // we create the model object 
       let downloadedEvent = EventModel() 
       /* we have new data remove old data from core data*/ 
       coreDataObject.deleteData(entityArgument: "Event") 
       events.removeAll() // remove the data from array this is no longer needed FIX 
       for JSONData in response.result.value as! [Dictionary<String, Any>] { 
        /* get the data from JSON and store in the event model*/ 
        downloadedEvent.eTitle = JSONData[downloadedEvent.titleString] as? String 
        downloadedEvent.eDate = JSONData[downloadedEvent.dateString] as? String 
        downloadedEvent.eDescription = JSONData[downloadedEvent.descriptionString] as? String 
        downloadedEvent.eLocation = JSONData[downloadedEvent.locationline1String] as? String 
        downloadedEvent.eLocation2 = JSONData[downloadedEvent.locationline2String] as? String 
        /* if the event has an image save the url*/ 
        if let image = JSONData[downloadedEvent.imageString] as? String { 
         downloadedEvent.eImageURL = image 
        } else { 
         /* else we save the default image url */ 
         downloadedEvent.eImageURL = downloadedEvent.defaultImageURL 
        } 
        coreDataObject.save(eventParam: downloadedEvent) 
       } 
       /* post notification to reload table view FIX */ 
       NotificationCenter.default.post(name: RELOAD_NOTIFICATION, object: nil) 
      case .failure(let error): 
       print("ALAMO REQUEST FIALED: \(error)") 
      } 
    } 
} 
+0

必要に何をすべきか'' .success: '< - あなたはここで成功すると*完了します。あなたの要求が成功しなかった場合、あなたは' .failure(エラーにしましょう) 'に戻ります。 – Honey

答えて

1

は、それが成功した発信者を通知する機能を持つdownloadEvents機能である:

func downloadEvents(completion: @escaping (Bool, String?)-> Void) { 
    let coreDataObject = CoreDataMethods() 
    Alamofire.request(URL(string:URL)!) 
     .responseJSON { response in 

      switch response.result { 
      case .success: 
       // we create the model object 
       let downloadedEvent = EventModel() 
       /* we have new data remove old data from core data*/ 
       coreDataObject.deleteData(entityArgument: "Event") 
       events.removeAll() // remove the data from array this is no longer needed FIX 
       for JSONData in response.result.value as! [Dictionary<String, Any>] { 
        /* get the data from JSON and store in the event model*/ 
        downloadedEvent.eTitle = JSONData[downloadedEvent.titleString] as? String 
        downloadedEvent.eDate = JSONData[downloadedEvent.dateString] as? String 
        downloadedEvent.eDescription = JSONData[downloadedEvent.descriptionString] as? String 
        downloadedEvent.eLocation = JSONData[downloadedEvent.locationline1String] as? String 
        downloadedEvent.eLocation2 = JSONData[downloadedEvent.locationline2String] as? String 
        /* if the event has an image save the url*/ 
        if let image = JSONData[downloadedEvent.imageString] as? String { 
         downloadedEvent.eImageURL = image 
        } else { 
         /* else we save the default image url */ 
         downloadedEvent.eImageURL = downloadedEvent.defaultImageURL 
        } 
        coreDataObject.save(eventParam: downloadedEvent) 
       } 

       completion(true, nil) 

       /* post notification to reload table view FIX */ 
       //NotificationCenter.default.post(name: RELOAD_NOTIFICATION, object: nil) 
      case .failure(let error): 
       print("ALAMO REQUEST FIALED: \(error)") 
       completion(false, "ALAMO REQUEST FIALED: \(error)") 
      } 
    } 
} 

あなたは、このような関数を呼び出します。

func reloadTable(){ 

    downloadEvents { (success, errMsg) in 
     if success{ 
      DispatchQueue.main.async { 
       self.tableView.reloadData() 
      } 
     } 
     else{ 
      let alertMessage: String 
      if let err = errMsg{ 
       alertMessage = err 
      } 
      else{ 
       alertMessage = "An unknown error occurred." 
      } 
      let alert = UIAlertController.init(title: "Request Failed", message: alertMessage, preferredStyle: .alert) 
      alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) 
      DispatchQueue.main.async { 
       self.present(alert, animated: true, completion: nil) 
      } 
     } 
    } 
} 
関連する問題