Fix ASDKgram example #trivial (#700)

- Fix an insta-crash that's caused by Webservice.load method to call its completion block off the main thread.
- Fix incorrect http status code check.
- Bump the deployment target to get the project compiling.
This commit is contained in:
Stephen Williams 2017-12-21 01:53:13 +13:00 committed by Huy Nguyen
parent fff5aae0a5
commit 4dec51ca37
2 changed files with 10 additions and 9 deletions

View File

@ -428,7 +428,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = YES; MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES; ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos; SDKROOT = iphoneos;
@ -478,7 +478,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO; MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos; SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

View File

@ -25,12 +25,13 @@ final class WebService {
URLSession.shared.dataTask(with: resource.url) { data, response, error in URLSession.shared.dataTask(with: resource.url) { data, response, error in
// Check for errors in responses. // Check for errors in responses.
let result = self.checkForNetworkErrors(data, response, error) let result = self.checkForNetworkErrors(data, response, error)
DispatchQueue.main.async {
switch result { switch result {
case .success(let data): case .success(let data):
completion(resource.parse(data)) completion(resource.parse(data))
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
}
} }
}.resume() }.resume()
} }
@ -49,7 +50,7 @@ extension WebService {
} }
} }
if let response = response as? HTTPURLResponse, response.statusCode >= 200 && response.statusCode <= 299 { if let response = response as? HTTPURLResponse, response.statusCode <= 200 && response.statusCode >= 299 {
return .failure((.invalidStatusCode("Request returned status code other than 2xx \(response)"))) return .failure((.invalidStatusCode("Request returned status code other than 2xx \(response)")))
} }