Cappuccino90 icon

Untitled

Cappuccino90 | PRO | 07/21/16 10:41:23 PM UTC | 0 ⭐ | 218 👁️ | Never ⏰ | []
Java |

3.86 KB

|

None

|

0 👍

/

0 👎

    func jsonParser() {
        let urlPath = "https://api.github.com/users/\(githubName)/repos"
        
        guard let endpoint = NSURL(string: urlPath) else {
            print("Error creating endpoint")
            let alert = UIAlertController(title: "Error Github Name Request", message: "Error creating endpoint", preferredStyle: UIAlertControllerStyle.Alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                UIAlertAction in
                self.navigationController?.popToRootViewControllerAnimated(true)
            }
            alert.addAction(okAction)
            self.presentViewController(alert, animated: true, completion: nil);
            
            return
        }
        
        
       // How would I implement this part of the code, to make the response asynchronous? 
    
        let queue = NSOperationQueue()
        let o = op()
        
        queue.addOperation(o.parseResponse(data: NSData?, response: NSURLResponse?, error: NSError?, completionHandler: (parsedData: [ViewControllerTableView.Repository], error: NSError) -> ()))
        
 
 
 
 
        let request = NSMutableURLRequest(URL:endpoint)
        NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
            do {
                guard let data = data else {
                    let alert = UIAlertController(title: "Error Github Name Request", message: "\(JSONError.NoData)", preferredStyle: UIAlertControllerStyle.Alert)
                    
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                        UIAlertAction in
                        self.navigationController?.popToRootViewControllerAnimated(true)
                    }
                    
                    alert.addAction(okAction)
                    self.presentViewController(alert, animated: true, completion: nil);
                    throw JSONError.NoData
                }
                
                guard let json = try NSJSONSerialization.JSONObjectWithData(data, options:.AllowFragments) as? [[String: AnyObject]] else {
                    
                    let alert = UIAlertController(title: "Error Github Name Request", message: "\(JSONError.ConversionFailed)", preferredStyle: UIAlertControllerStyle.Alert)
                    
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                        UIAlertAction in
                        self.navigationController?.popToRootViewControllerAnimated(true)
                    }
                    alert.addAction(okAction)
                    self.presentViewController(alert, animated: true, completion: nil);
                    throw JSONError.ConversionFailed
                }
                
                self.owner=json[0]["owner"]!["login"]! as! String
                self.allRepos.removeAll()
                for a in json {
                    let temp:Repository = Repository(id: (a["id"] as? Int)!,name: (a["name"] as? String), size: (a["size"] as? Int)!, watchers: (a["watchers"] as? Int), created_at: (a["created_at"] as? String)!, descr: (a["description"] as? String)!)
                    self.allRepos.append(temp)
                }
                self.tableRefresh(self.allRepos)
                
            } catch let error as JSONError {
                print(error.rawValue)
            } catch let error as NSError {
                print(error.debugDescription)
            }
            }.resume()
 
    }
    
    
    
    class op: NSBlockOperation {
 
        func parseResponse(data: NSData?, response:
            NSURLResponse?,error:
            NSError?,completionHandler: (parsedData:
            [Repository],error: NSError) -> ())
        {
            
 
        } 
    }

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎