等待GET请求完成

时间:2017-03-13 15:51:34

标签: swift swift3 alamofire

我试图找出如何告诉应用程序等待一个任务完成。我需要做两个休息请求,他们互相依赖。第二个需要第一个数据才能成功。

所以我做测试请求的方式是:

private DateTime Date { get; set; }
public DateTimeOffset DateOffset
{
    get { return DateTime.SpecifyKind(Date, DateTimeKind.Utc); }
    set { Date = value.DateTime; }
}

第二次获取需要第一次看起来像这样的数据 - 只有更改是URL。

然后我想在我的应用程序中的某处调用这两个函数,并使用反序列化的数据进行smth。

func get(completion: @escaping (Any?) -> Void){
        Alamofire.request(URL, method: .get, parameters: ["token": TOKEN]).validate().responseJSON { response in
            if response.result.value != nil {
                // DESERIALIZE IT INTO ARRAY " objects"
                }
                completion(objects)
            } else {
                completion(nil)
            }
        }
    }

所以我想告诉应用程序等待第一个请求在下一个启动之前完成,因为它将在{for循环中func loadData() { Service.Instance.get{ (json) in guard json != nil else { print("Error / json = nil pointer") return } self.data = json as! [Data] } for dt in self.data { Service.Instance.get(data: dt, offset: 0, limit: 10, completion: { (json2) in guard json2 != nil else { print("Error / json = nil pointer") return } if dt.count > 0 { self.objects = json2 as! [VehicleModel]! self.fulldata.append(self.objects) } else { print("There is no object in the Array") } }) } } 崩溃。

0 个答案:

没有答案
相关问题